home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / libast.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-18  |  107.5 KB  |  2,865 lines

  1. /*
  2.  * Copyright (C) 1997-2004, Michael Jennings
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  5.  * of this software and associated documentation files (the "Software"), to
  6.  * deal in the Software without restriction, including without limitation the
  7.  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8.  * sell copies of the Software, and to permit persons to whom the Software is
  9.  * furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice shall be included in
  12.  * all copies of the Software, its documentation and marketing & publicity
  13.  * materials, and acknowledgment shall be given in the documentation, materials
  14.  * and software packages that this Software was used.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19.  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20.  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.  */
  23.  
  24. /**
  25.  * @file libast.h
  26.  * Global LibAST header file.
  27.  *
  28.  * This file contains all general-purpose macros, function
  29.  * declarations, etc. for LibAST.  It is also responsible for
  30.  * including all required system headers and LibAST Object headers.
  31.  *
  32.  * @author Michael Jennings <mej@eterm.org>
  33.  * @version $Revision: 1.57 $
  34.  * @date $Date: 2004/10/26 18:01:51 $
  35.  */
  36.  
  37. #ifndef _LIBAST_H_
  38. #define _LIBAST_H_
  39.  
  40. #include <libast/sysdefs.h>
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <time.h>
  45. #include <sys/stat.h>
  46. #include <sys/wait.h>
  47. #include <unistd.h>
  48. #include <ctype.h>
  49. #include <string.h>
  50. #include <fcntl.h>
  51. #include <dirent.h>
  52. #include <errno.h>
  53. #include <signal.h>
  54. #include <limits.h>
  55. #include <math.h>
  56. #if TIME_WITH_SYS_TIME
  57. # include <sys/time.h>
  58. #endif
  59. #if WITH_DMALLOC
  60. # include <dmalloc.h>
  61. #elif HAVE_MALLOC_H
  62. # include <malloc.h>
  63. #endif
  64.  
  65. #include <netdb.h>
  66. #include <sys/types.h>
  67. #include <sys/socket.h>
  68. #include <netinet/in.h>
  69. #include <sys/un.h>
  70. #include <arpa/inet.h>
  71. #include <netinet/tcp.h>
  72. #include <netinet/udp.h>
  73.  
  74. #if LIBAST_X11_SUPPORT
  75. # include <X11/Xatom.h>
  76. # include <X11/X.h>
  77. # include <X11/Intrinsic.h>
  78. # if LIBAST_IMLIB2_SUPPORT
  79. #  include <Imlib2.h>
  80. # endif
  81. #endif
  82.  
  83. #ifndef __GNUC__
  84. #  define __attribute__(x)
  85. #  define __extension__(x)
  86. #endif
  87.  
  88. #if LIBAST_REGEXP_SUPPORT_PCRE
  89. #  if HAVE_PCRE_H
  90. #    include <pcre.h>
  91. #  elif HAVE_PCRE_PCRE_H
  92. #    include <pcre/pcre.h>
  93. #  endif
  94. #elif LIBAST_REGEXP_SUPPORT_POSIX || LIBAST_REGEXP_SUPPORT_BSD
  95. #  if HAVE_REGEX_H
  96. #    include <regex.h>
  97. #  endif
  98. #  if LIBAST_REGEXP_SUPPORT_BSD
  99. extern char *re_comp();
  100. extern int re_exec();
  101. #  endif
  102. #endif
  103.  
  104. #include <libast/types.h>
  105. #include <libast/obj.h>
  106. #include <libast/objpair.h>
  107. #include <libast/regexp.h>
  108. #include <libast/socket.h>
  109. #include <libast/str.h>
  110. #include <libast/tok.h>
  111. #include <libast/url.h>
  112.  
  113. #include <libast/iterator_if.h>
  114. #include <libast/list_if.h>
  115. #include <libast/map_if.h>
  116. #include <libast/vector_if.h>
  117.  
  118. #include <libast/array.h>
  119. #include <libast/linked_list.h>
  120. #include <libast/dlinked_list.h>
  121.  
  122. #include <libast/avl_tree.h>
  123.  
  124. /******************************* GENERIC GOOP *********************************/
  125. /**
  126.  * Mark a variable as used.
  127.  *
  128.  * This macro is used to explicitly mark a variable as "used."  It
  129.  * intentionally generates no real code, but suppresses gcc warnings
  130.  * about unused variables and/or parameters.  That way, the programmer
  131.  * can explicitly acknowledge that certain variables/parameters are
  132.  * intentionally unused, making the warnings more effective by
  133.  * eliminating false positives.
  134.  *
  135.  * @param x Any variable or parameter name.
  136.  */
  137. #define USE_VAR(x)   (void) x
  138.  
  139. /**
  140.  * @def MIN(a, b)
  141.  * Return the lesser of @a a or @a b.
  142.  *
  143.  * This macro compares its two parameters, @a a and @a b, and returns
  144.  * the lesser of the two (the minimum).  When building under gcc, a
  145.  * GNU-specific extension is used which prevents expressions used as
  146.  * parameters from being evaluated multiple times.
  147.  *
  148.  * @param a Any expression that evaluates to a value.
  149.  * @param b Any expression that evaluates to a value.
  150.  * @return The lesser of the two values.
  151.  */
  152. /**
  153.  * @def MAX(a, b)
  154.  * Return the greater of @a a or @a b.
  155.  *
  156.  * This macro compares its two parameters, @a a and @a b, and returns
  157.  * the greater of the two (the maximum).  When building under gcc, a
  158.  * GNU-specific extension is used which prevents expressions used as
  159.  * parameters from being evaluated multiple times.
  160.  *
  161.  * @param a Any expression that evaluates to a value.
  162.  * @param b Any expression that evaluates to a value.
  163.  * @return The greater of the two values.
  164.  */
  165. /**
  166.  * @def LOWER_BOUND(current, other)
  167.  * Force a lower bound on a variable.
  168.  *
  169.  * This macro checks the value of its first parameter, @a current, and
  170.  * makes sure it is greater than or equal to the value of @a other.
  171.  * If @a current is less than @a other, @a current is assigned the
  172.  * value of @a other.  In essence, this establishes a "lower bound" on
  173.  * @a current equal to the value of @a other.
  174.  *
  175.  * @param current The variable to check.
  176.  * @param other   The value by which @a current will be bound.
  177.  * @return The new value of @a current.
  178.  */
  179. /**
  180.  * @def UPPER_BOUND(current, other)
  181.  * Force an upper bound on a variable.
  182.  *
  183.  * This macro checks the value of its first parameter, @a current, and
  184.  * makes sure it is less than or equal to the value of @a other.  If
  185.  * @a current is greater than @a other, @a current is assigned the
  186.  * value of @a other.  In essence, this establishes an "upper bound"
  187.  * on @a current equal to the value of @a other.
  188.  *
  189.  * @param current The variable to check.
  190.  * @param other   The value by which @a current will be bound.
  191.  * @return The new value of @a current.
  192.  */
  193. /**
  194.  * @def BOUND(val, min, max)
  195.  * Force a variable to be within a given range.
  196.  *
  197.  * This macro checks the value of its first parameter, @a val, and
  198.  * makes sure it is between @a min and @a max, inclusive.  If @a val
  199.  * is above this range, it is assigned the value of @a max.  Likewise,
  200.  * if @a val is below this range, it is assigned the value of @a min.
  201.  * In essence, this establishes both an "upper bound" and a "lower
  202.  * bound" on @a val.
  203.  *
  204.  * @param val The variable to check.
  205.  * @param min The lowest value @a val may have.
  206.  * @param max The highest value @a val may have.
  207.  * @return The new value of @a val.
  208.  */
  209. #ifdef MIN
  210. # undef MIN
  211. #endif
  212. #ifdef MAX
  213. # undef MAX
  214. #endif
  215. #ifdef __GNUC__
  216. # define MIN(a,b)                       __extension__ ({__typeof__(a) aa = (a); __typeof__(b) bb = (b); (aa < bb) ? (aa) : (bb);})
  217. # define MAX(a,b)                       __extension__ ({__typeof__(a) aa = (a); __typeof__(b) bb = (b); (aa > bb) ? (aa) : (bb);})
  218. # define LOWER_BOUND(current, other)    __extension__ ({__typeof__(other) o = (other); ((current) < o) ? ((current) = o) : (current);})
  219. # define UPPER_BOUND(current, other)    __extension__ ({__typeof__(other) o = (other); ((current) > o) ? ((current) = o) : (current);})
  220. # define BOUND(val, min, max)           __extension__ ({__typeof__(min) m1 = (min); __typeof__(max) m2 = (max); ((val) < m1) ? ((val) = m1) : (((val) > m2) ? ((val) = m2) : (val));})
  221. #else
  222. # define MIN(a,b)                       (((a) < (b)) ? (a) : (b))
  223. # define MAX(a,b)                       (((a) > (b)) ? (a) : (b))
  224. # define LOWER_BOUND(current, other)    (((current) < (other)) ? ((current) = (other)) : (current))
  225. # define UPPER_BOUND(current, other)    (((current) > (other)) ? ((current) = (other)) : (current))
  226. # define BOUND(val, min, max)           (((val) < (min)) ? ((val) = (min)) : (((val) > (max)) ? ((val) = (max)) : (val)))
  227. #endif
  228. /** @def AT_LEAST(current, other) Alias for LOWER_BOUND().  This macro is an alias for LOWER_BOUND(). */
  229. #define AT_LEAST(current, other)        LOWER_BOUND(current, other)
  230. /** @def MAX_IT(current, other) Alias for LOWER_BOUND().  This macro is an alias for LOWER_BOUND(). */
  231. #define MAX_IT(current, other)          LOWER_BOUND(current, other)
  232. /** @def AT_MOST(current, other) Alias for UPPER_BOUND().  This macro is an alias for UPPER_BOUND(). */
  233. #define AT_MOST(current, other)         UPPER_BOUND(current, other)
  234. /** @def MIN_IT(current, other) Alias for UPPER_BOUND().  This macro is an alias for UPPER_BOUND(). */
  235. #define MIN_IT(current, other)          UPPER_BOUND(current, other)
  236. /** @def CONTAIN(val, min, max) Alias for BOUND().  This macro is an alias for BOUND(). */
  237. #define CONTAIN(val, min, max)          BOUND(val, min, max)
  238. /**
  239.  * Swaps two values.
  240.  *
  241.  * This macro swaps the values of its first two parameters using the
  242.  * third as temporary storage.
  243.  *
  244.  * @param one The first variable.
  245.  * @param two The second variable.
  246.  * @param tmp A temporary holding spot used during swapping.
  247.  */
  248. #define SWAP_IT(one, two, tmp)          do {(tmp) = (one); (one) = (two); (two) = (tmp);} while (0)
  249.  
  250. /**
  251.  * @def SWAP(a, b)
  252.  * Swaps two values.
  253.  *
  254.  * This macro performs the same task as the SWAP_IT() macro, except
  255.  * that no temporary variable is required.  Instead, a temporary
  256.  * variable is created by the macro itself.  Under gcc, the
  257.  * __typeof__() extension is used to create a temporary variable of
  258.  * the same type as @a a.  Under other compilers, a void pointer is
  259.  * used.
  260.  *
  261.  * @param a The first variable.
  262.  * @param b The second variable.
  263.  */
  264. #ifdef __GNUC__
  265. # define SWAP(a, b)  (void) __extension__ ({__typeof__(a) __tmp = (a); (a) = (b); (b) = __tmp;})
  266. #else
  267. # define SWAP(a, b)  do {void *__tmp = ((void *)(a)); (a) = (b); (b) = __tmp;} while (0)
  268. #endif
  269. /**
  270.  * Swaps two values.
  271.  *
  272.  * This macro swaps the values of @a a and @a b using the now-infamous
  273.  * chained XOR trick.
  274.  *
  275.  * @attention ONLY use this with like variables, and only those which
  276.  * can safely be cast to and from a long.  If you're unsure of whether
  277.  * or not it would be safe, use SWAP() or SWAP_IT() instead!
  278.  *
  279.  * @param a The first variable.
  280.  * @param b The second variable.
  281.  */
  282. #if STRICT_ISO_C99
  283. #  define BINSWAP(a, b)  SWAP(a, b)
  284. #else
  285. #  define BINSWAP(a, b)  (((long) (a)) ^= ((long) (b)) ^= ((long) (a)) ^= ((long) (b)))
  286. #endif
  287.  
  288. /**
  289.  * Make sure a char pointer is non-NULL before printing it.
  290.  *
  291.  * This is a convenience macro primarily targetted at systems like
  292.  * Solaris where doing a printf() on a NULL char pointer using %s
  293.  * results in a segmentation fault rather than helpful message.  This
  294.  * macro should be used in any place where a string is printed which
  295.  * could potentially be NULL.
  296.  *
  297.  * @param x A string (char *).
  298.  * @return @a x, or if @a x is NULL, the string "<@a x null>"
  299.  */
  300. #define NONULL(x) (((char *) (x)) ? ((char *) (x)) : ((char *) ("<" #x " null>")))
  301.  
  302. /**
  303.  * Not-A-Number
  304.  *
  305.  * This makes sure NAN is defined.
  306.  */
  307. #ifndef NAN
  308. #  ifdef MAX_FLOAT
  309. #    define NAN MAX_FLOAT
  310. #  elsif defined(MAXFLOAT)
  311. #    define NAN MAX_FLOAT
  312. #  elsif defined(HUGE)
  313. #    define NAN HUGE
  314. #  else
  315. /* FIXME:  This could be dangerous...anyone have a better idea? */
  316. #    define NAN 3.40282347e+38F
  317. #  endif
  318. #endif
  319.  
  320. /****************************** DEBUGGING GOOP ********************************/
  321. #ifndef LIBAST_DEBUG_FD
  322. /**
  323.  * Where to send debugging output.
  324.  *
  325.  * This defines where debugging output should be sent.  Should be
  326.  * either stdout or stderr.
  327.  *
  328.  * @ingroup DOXGRP_DEBUG
  329.  */
  330. # define LIBAST_DEBUG_FD  (stderr)
  331. #endif
  332. #ifndef DEBUG
  333. /**
  334.  * Maximum compile-time debugging level.
  335.  *
  336.  * LibAST supports debugging levels, allowing for progressively more
  337.  * verbosity of debugging output as the level gets higher.  This
  338.  * defines the compile-time maximum; support for higher debugging
  339.  * levels than this will not even be compiled in, so use care when
  340.  * setting this.
  341.  *
  342.  * @ingroup DOXGRP_DEBUG
  343.  */
  344. # define DEBUG 0
  345. #endif
  346.  
  347. /** UNDOCUMENTED. */
  348. #define DEBUG_LEVEL       (libast_debug_level)
  349. /** UNDOCUMENTED. */
  350. #define DEBUG_FLAGS       (libast_debug_flags)
  351.  
  352. /** Does nothing.  This macro is a nop (no operation).  It does nothing. */
  353. #define NOP ((void)0)
  354.  
  355. /**
  356.  * A fix-me NOP.
  357.  *
  358.  * This is the same as NOP(), but is used to mark something needing to
  359.  * be fixed.
  360.  */
  361. #define FIXME_NOP(x)
  362. /**
  363.  * Mark a block of code needing fixing.
  364.  *
  365.  * This marks a block of code needing fixing and removes it.
  366.  */
  367. #define FIXME_BLOCK 0
  368.  
  369. /**
  370.  * Mark unused blocks of code.
  371.  *
  372.  * This marks a block of code as unused and removes it.
  373.  */
  374. #define UNUSED_BLOCK 0
  375.  
  376. /**
  377.  * @def __DEBUG()
  378.  * Format and print debugging output.
  379.  *
  380.  * This macro formats and prints debugging output by prepending a
  381.  * timestamp, the filename, the line number, and (if available) the
  382.  * function name.
  383.  *
  384.  * This is an internal macro and should not be used directly.
  385.  * @ingroup DOXGRP_DEBUG
  386.  */
  387. #if defined(__FILE__) && defined(__LINE__)
  388. # ifdef __GNUC__
  389. #  define __DEBUG()  fprintf(LIBAST_DEBUG_FD, "[%lu] %12s | %4d: %s(): ", (unsigned long) time(NULL), __FILE__, __LINE__, __FUNCTION__)
  390. # else
  391. #  define __DEBUG()  fprintf(LIBAST_DEBUG_FD, "[%lu] %12s | %4d: ", (unsigned long) time(NULL), __FILE__, __LINE__)
  392. # endif
  393. #else
  394. # define __DEBUG()   NOP
  395. #endif
  396.  
  397. /**
  398.  * Assert reaching a line of code.
  399.  *
  400.  * This macro is simply a quick-and-dirty way of printing out a unique
  401.  * message which proves that a particular portion of code was reached
  402.  * and executed properly.
  403.  *
  404.  * @ingroup DOXGRP_DEBUG
  405.  */
  406. #define MOO()  do {__DEBUG(); libast_dprintf("Moo.\n");} while (0)
  407.  
  408. /**
  409.  * @def ASSERT(x)
  410.  * Asserts that a condition is true.
  411.  *
  412.  * This macro evaluates an expression, @a x, and takes action if the
  413.  * expression evaluates to false (0).  It works similarly to the libc
  414.  * function assert(), with the exception that it will not call abort()
  415.  * if the assertion fails.  Instead, it will either issue a fatal
  416.  * error (generally resulting in a backtrace) if debugging is active,
  417.  * or print a warning if it is not.  In either event, the warning/error
  418.  * message will contain the filename, line number, and (if available)
  419.  * function name where the error occured.
  420.  *
  421.  * If only a warning is generated, the function will return
  422.  * immediately.
  423.  *
  424.  * @param x Any valid boolean expression.
  425.  * @ingroup DOXGRP_DEBUG
  426.  */
  427. /**
  428.  * @def ASSERT_RVAL(x, val)
  429.  * Asserts that a condition is true, and provides a return value in
  430.  * case it isn't.
  431.  *
  432.  * This macro is identical to ASSERT(), except that it returns a
  433.  * value, @a val, instead of returning void.
  434.  *
  435.  * @param x   Any valid boolean expression.
  436.  * @param val The return value to use if @a x evaluates to false.
  437.  * @ingroup DOXGRP_DEBUG
  438.  */
  439. /**
  440.  * @def ASSERT_NOTREACHED()
  441.  * Asserts that a particular piece of code is not reached.
  442.  *
  443.  * This macro is used in sections of code that should never be
  444.  * reached.  Its actions are similar to those of ASSERT(), but instead
  445.  * of evaluating an expression, it always evaluates to false.
  446.  *
  447.  * @ingroup DOXGRP_DEBUG
  448.  */
  449. /**
  450.  * @def ASSERT_NOTREACHED_RVAL(val)
  451.  * Asserts that a particular piece of code is not reached, and
  452.  * provides a return value in case it is.
  453.  *
  454.  * This macro is identical to ASSERT_NOTREACHED(), except that it
  455.  * returns a value, @a val, instead of returning void.
  456.  *
  457.  * @ingroup DOXGRP_DEBUG
  458.  */
  459. /**
  460.  * @def ABORT()
  461.  * Throw a fatal exception.
  462.  *
  463.  * This macro is a replacement for the libc abort() function.  This
  464.  * version provides file/line/function information in the fatal error
  465.  * message.
  466.  *
  467.  * @ingroup DOXGRP_DEBUG
  468.  */
  469. /**
  470.  * @def REQUIRE(x)
  471.  * Return if an expression is false.
  472.  *
  473.  * This macro is similar to ASSERT(), except that @a x evaluating to
  474.  * false is not necessarily an error.  Normally, this macro simply
  475.  * causes the function to return.  However, if debugging is active, a
  476.  * message is printed noting the expression @a x and the location of
  477.  * the failure.  This macro is often used to test preconditions, such
  478.  * as making sure pointers are non-NULL before using them.
  479.  *
  480.  * @param x Any valid boolean expression.
  481.  * @ingroup DOXGRP_DEBUG
  482.  */
  483. /**
  484.  * @def REQUIRE_RVAL(x, v)
  485.  * Return @a v if an expression is false.
  486.  *
  487.  * This macro is identical to REQUIRE(), except that a return value
  488.  * for the function is supplied.
  489.  *
  490.  * @param x Any valid boolean expression
  491.  * @param v The function return value to use if @a x evaluates to
  492.  * false.
  493.  * @ingroup DOXGRP_DEBUG
  494.  */
  495. #if DEBUG >= 1
  496. # if defined(__FILE__) && defined(__LINE__)
  497. #  ifdef __GNUC__
  498. #   define ASSERT(x)  do {if (!(x)) {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed in %s() at %s:%d:  %s\n", __FUNCTION__, __FILE__, __LINE__, #x);} \
  499.                                                     else {libast_print_warning("ASSERT failed in %s() at %s:%d:  %s\n", __FUNCTION__, __FILE__, __LINE__, #x); return;}}} while (0)
  500. #   define ASSERT_RVAL(x, val)  do {if (!(x)) {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed in %s() at %s:%d:  %s\n", __FUNCTION__, __FILE__, __LINE__, #x);} \
  501.                                                               else {libast_print_warning("ASSERT failed in %s() at %s:%d:  %s\n", __FUNCTION__, __FILE__, __LINE__, #x);} \
  502.                                                return (val);}} while (0)
  503. #   define ASSERT_NOTREACHED()  do {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed in %s() at %s:%d:  This code should not be reached.\n", __FUNCTION__, __FILE__, __LINE__);} \
  504.                                                    else {libast_print_warning("ASSERT failed in %s() at %s:%d:  This code should not be reached.\n", __FUNCTION__, __FILE__, __LINE__);} \
  505.                                     } while (0)
  506. #   define ASSERT_NOTREACHED_RVAL(val)  do {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed in %s() at %s:%d:  This code should not be reached.\n", __FUNCTION__, __FILE__, __LINE__);} \
  507.                                                            else {libast_print_warning("ASSERT failed in %s() at %s:%d:  This code should not be reached.\n", __FUNCTION__, __FILE__, __LINE__);} \
  508.                                             return (val);} while (0)
  509. #   define ABORT() libast_fatal_error("Aborting in %s() at %s:%d.\n", __FUNCTION__, __FILE__, __LINE__)
  510. #  else
  511. #   define ASSERT(x)  do {if (!(x)) {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed at %s:%d:  %s\n", __FILE__, __LINE__, #x);} \
  512.                                                     else {libast_print_warning("ASSERT failed at %s:%d:  %s\n", __FILE__, __LINE__, #x); return;}}} while (0)
  513. #   define ASSERT_RVAL(x, val)  do {if (!(x)) {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed at %s:%d:  %s\n", __FILE__, __LINE__, #x);} \
  514.                                                               else {libast_print_warning("ASSERT failed at %s:%d:  %s\n", __FILE__, __LINE__, #x);} \
  515.                                                return (val);}} while (0)
  516. #   define ASSERT_NOTREACHED()  do {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed at %s:%d:  This code should not be reached.\n", __FILE__, __LINE__);} \
  517.                                                    else {libast_print_warning("ASSERT failed at %s:%d:  This code should not be reached.\n", __FILE__, __LINE__);} \
  518.                                     } while (0)
  519. #   define ASSERT_NOTREACHED_RVAL(val)  do {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed at %s:%d:  This code should not be reached.\n", __FILE__, __LINE__);} \
  520.                                                            else {libast_print_warning("ASSERT failed at %s:%d:  This code should not be reached.\n", __FILE__, __LINE__);} \
  521.                                             return (val);} while (0)
  522. #   define ABORT() libast_fatal_error("Aborting at %s:%d.\n", __FILE__, __LINE__)
  523. #  endif
  524. # else
  525. #  define ASSERT(x)  do {if (!(x)) {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed:  %s\n", #x);} \
  526.                                                    else {libast_print_warning("ASSERT failed:  %s\n", #x); return;}}} while (0)
  527. #  define ASSERT_RVAL(x, val)  do {if (!(x)) {if (DEBUG_LEVEL>=1) {libast_fatal_error("ASSERT failed:  %s\n", #x);} \
  528.                                                              else {libast_print_warning("ASSERT failed:  %s\n", #x);} return (val);}} while (0)
  529. #  define ASSERT_NOTREACHED()           return
  530. #  define ASSERT_NOTREACHED_RVAL(x)     return (x)
  531. #  define ABORT()                       libast_fatal_error("Aborting.\n")
  532. # endif
  533. # define REQUIRE(x)                     do {if (!(x)) {if (DEBUG_LEVEL>=1) {__DEBUG(); libast_dprintf("REQUIRE failed:  %s\n", #x);} return;}} while (0)
  534. # define REQUIRE_RVAL(x, v)             do {if (!(x)) {if (DEBUG_LEVEL>=1) {__DEBUG(); libast_dprintf("REQUIRE failed:  %s\n", #x);} return (v);}} while (0)
  535. #else
  536. # define ASSERT(x)                      NOP
  537. # define ASSERT_RVAL(x, val)            NOP
  538. # define ASSERT_NOTREACHED()            return
  539. # define ASSERT_NOTREACHED_RVAL(val)    return (val)
  540. # define ABORT()                        libast_fatal_error("Aborting.\n")
  541. # define REQUIRE(x)                     do {if (!(x)) return;} while (0)
  542. # define REQUIRE_RVAL(x, v)             do {if (!(x)) return (v);} while (0)
  543. #endif
  544.  
  545. /**
  546.  * @def DPRINTF(x)
  547.  * Print debugging output.
  548.  *
  549.  * This macro can be used for unconditional debugging output.  If any
  550.  * level of debugging support has been compiled in, this macro will
  551.  * print a debugging message.
  552.  *
  553.  * This macro will almost never be used directly; instead, use the
  554.  * D_*() macros.
  555.  *
  556.  * @attention Calls to this and other debugging output macros
  557.  * MUST be double-parenthesized, like so:  DPRINTF((...));
  558.  *
  559.  * @param x A parenthesized argument list suitable for a printf-style
  560.  *          function.
  561.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  562.  * @ingroup DOXGRP_DEBUG
  563.  */
  564. /**
  565.  * @def DPRINTF1(x)
  566.  * Print level 1 debugging output.
  567.  *
  568.  * This macro is identical to DPRINTF(), except that the message will
  569.  * only be printed if the debug level is 1 or higher.
  570.  *
  571.  * @param x A parenthesized argument list suitable for a printf-style
  572.  *          function.
  573.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  574.  * @ingroup DOXGRP_DEBUG
  575.  */
  576. /**
  577.  * @def DPRINTF2(x)
  578.  * Print level 2 debugging output.
  579.  *
  580.  * This macro is identical to DPRINTF(), except that the message will
  581.  * only be printed if the debug level is 2 or higher.
  582.  *
  583.  * @param x A parenthesized argument list suitable for a printf-style
  584.  *          function.
  585.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  586.  * @ingroup DOXGRP_DEBUG
  587.  */
  588. /**
  589.  * @def DPRINTF3(x)
  590.  * Print level 3 debugging output.
  591.  *
  592.  * This macro is identical to DPRINTF(), except that the message will
  593.  * only be printed if the debug level is 3 or higher.
  594.  *
  595.  * @param x A parenthesized argument list suitable for a printf-style
  596.  *          function.
  597.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  598.  * @ingroup DOXGRP_DEBUG
  599.  */
  600. /**
  601.  * @def DPRINTF4(x)
  602.  * Print level 4 debugging output.
  603.  *
  604.  * This macro is identical to DPRINTF(), except that the message will
  605.  * only be printed if the debug level is 4 or higher.
  606.  *
  607.  * @param x A parenthesized argument list suitable for a printf-style
  608.  *          function.
  609.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  610.  * @ingroup DOXGRP_DEBUG
  611.  */
  612. /**
  613.  * @def DPRINTF5(x)
  614.  * Print level 5 debugging output.
  615.  *
  616.  * This macro is identical to DPRINTF(), except that the message will
  617.  * only be printed if the debug level is 5 or higher.
  618.  *
  619.  * @param x A parenthesized argument list suitable for a printf-style
  620.  *          function.
  621.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  622.  * @ingroup DOXGRP_DEBUG
  623.  */
  624. /**
  625.  * @def DPRINTF6(x)
  626.  * Print level 6 debugging output.
  627.  *
  628.  * This macro is identical to DPRINTF(), except that the message will
  629.  * only be printed if the debug level is 6 or higher.
  630.  *
  631.  * @param x A parenthesized argument list suitable for a printf-style
  632.  *          function.
  633.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  634.  * @ingroup DOXGRP_DEBUG
  635.  */
  636. /**
  637.  * @def DPRINTF7(x)
  638.  * Print level 7 debugging output.
  639.  *
  640.  * This macro is identical to DPRINTF(), except that the message will
  641.  * only be printed if the debug level is 7 or higher.
  642.  *
  643.  * @param x A parenthesized argument list suitable for a printf-style
  644.  *          function.
  645.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  646.  * @ingroup DOXGRP_DEBUG
  647.  */
  648. /**
  649.  * @def DPRINTF8(x)
  650.  * Print level 8 debugging output.
  651.  *
  652.  * This macro is identical to DPRINTF(), except that the message will
  653.  * only be printed if the debug level is 8 or higher.
  654.  *
  655.  * @param x A parenthesized argument list suitable for a printf-style
  656.  *          function.
  657.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  658.  * @ingroup DOXGRP_DEBUG
  659.  */
  660. /**
  661.  * @def DPRINTF9(x)
  662.  * Print level 9 debugging output.
  663.  *
  664.  * This macro is identical to DPRINTF(), except that the message will
  665.  * only be printed if the debug level is 9 or higher.
  666.  *
  667.  * @param x A parenthesized argument list suitable for a printf-style
  668.  *          function.
  669.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  670.  * @ingroup DOXGRP_DEBUG
  671.  */
  672. #if DEBUG >= 1
  673. # ifndef DPRINTF
  674. #  define DPRINTF(x)           do { __DEBUG(); libast_dprintf x; } while (0)
  675. # endif
  676. # define DPRINTF1(x)           do { if (DEBUG_LEVEL >= 1) {__DEBUG(); libast_dprintf x;} } while (0)
  677. # define DPRINTF2(x)           do { if (DEBUG_LEVEL >= 2) {__DEBUG(); libast_dprintf x;} } while (0)
  678. # define DPRINTF3(x)           do { if (DEBUG_LEVEL >= 3) {__DEBUG(); libast_dprintf x;} } while (0)
  679. # define DPRINTF4(x)           do { if (DEBUG_LEVEL >= 4) {__DEBUG(); libast_dprintf x;} } while (0)
  680. # define DPRINTF5(x)           do { if (DEBUG_LEVEL >= 5) {__DEBUG(); libast_dprintf x;} } while (0)
  681. # define DPRINTF6(x)           do { if (DEBUG_LEVEL >= 6) {__DEBUG(); libast_dprintf x;} } while (0)
  682. # define DPRINTF7(x)           do { if (DEBUG_LEVEL >= 7) {__DEBUG(); libast_dprintf x;} } while (0)
  683. # define DPRINTF8(x)           do { if (DEBUG_LEVEL >= 8) {__DEBUG(); libast_dprintf x;} } while (0)
  684. # define DPRINTF9(x)           do { if (DEBUG_LEVEL >= 9) {__DEBUG(); libast_dprintf x;} } while (0)
  685. #else
  686. # ifndef DPRINTF
  687. #  define DPRINTF(x)           NOP
  688. # endif
  689. # define DPRINTF1(x)           NOP
  690. # define DPRINTF2(x)           NOP
  691. # define DPRINTF3(x)           NOP
  692. # define DPRINTF4(x)           NOP
  693. # define DPRINTF5(x)           NOP
  694. # define DPRINTF6(x)           NOP
  695. # define DPRINTF7(x)           NOP
  696. # define DPRINTF8(x)           NOP
  697. # define DPRINTF9(x)           NOP
  698. #endif
  699.  
  700. /**
  701.  * Debugging output you (almost) never want.
  702.  *
  703.  * This macro is used for mapping debugging output you almost never
  704.  * want to see.  Map D_*() macros to this for overly verbose or
  705.  * problematic debugging information, then manually redefine this as
  706.  * needed.
  707.  *
  708.  * @param x A parenthesized argument list suitable for a printf-style
  709.  *          function.
  710.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  711.  * @ingroup DOXGRP_DEBUG
  712.  */
  713. #define D_NEVER(x)             NOP
  714.  
  715. /** Set options debugging to level 1.  @see @link DOXGRP_DEBUG Debugging Subsystem @endlink */
  716. #define DEBUG_OPTIONS          1
  717. /**
  718.  * Option debugging macro.
  719.  *
  720.  * This macro is used for debugging output related to the options
  721.  * subsystem.  It maps to DPRINTF1() so that options-related debugging
  722.  * output will occur at debug level 1 and higher.
  723.  *
  724.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  725.  * @ingroup DOXGRP_DEBUG
  726.  */
  727. #if DEBUG >= DEBUG_OPTIONS
  728. #  define D_OPTIONS_IF           if (DEBUG_LEVEL >= DEBUG_OPTIONS)
  729. #  define D_OPTIONS(x)           do { D_OPTIONS_IF {DPRINTF(x);} } while (0)
  730. #else
  731. #  define D_OPTIONS_IF           if (0)
  732. #  define D_OPTIONS(x)           NOP
  733. #endif
  734.  
  735. /** Set object system debugging to level 2.  @see @link DOXGRP_DEBUG Debugging Subsystem @endlink */
  736. #define DEBUG_OBJ                2
  737. /**
  738.  * Object debugging macro.
  739.  *
  740.  * This macro is used for debugging output related to the object
  741.  * subsystem.  It maps to DPRINTF2() so that object-related debugging
  742.  * output will occur at debug level 2 and higher.
  743.  *
  744.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  745.  * @ingroup DOXGRP_DEBUG
  746.  */
  747. #if DEBUG >= DEBUG_OBJ
  748. #  define D_OBJ_IF               if (DEBUG_LEVEL >= DEBUG_OBJ)
  749. #  define D_OBJ(x)               do { D_OBJ_IF {DPRINTF(x);} } while (0)
  750. #else
  751. #  define D_OBJ_IF               if (0)
  752. #  define D_OBJ(x)               NOP
  753. #endif
  754.  
  755. /** Set config file parser debugging to level 3.  @see @link DOXGRP_DEBUG Debugging Subsystem @endlink */
  756. #define DEBUG_CONF               3
  757. /**
  758.  * Config file parser debugging macro.
  759.  *
  760.  * This macro is used for debugging output related to the config file
  761.  * parser.  It maps to DPRINTF3() so that config-related debugging
  762.  * output will occur at debug level 3 and higher.
  763.  *
  764.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  765.  * @ingroup DOXGRP_DEBUG
  766.  */
  767. #if DEBUG >= DEBUG_CONF
  768. #  define D_CONF_IF              if (DEBUG_LEVEL >= DEBUG_CONF)
  769. #  define D_CONF(x)              do { D_CONF_IF {DPRINTF(x);} } while (0)
  770. #else
  771. #  define D_CONF_IF              if (0)
  772. #  define D_CONF(x)              NOP
  773. #endif
  774.  
  775. /** Set memory allocation debugging to level 5.  @see @link DOXGRP_DEBUG Debugging Subsystem @endlink */
  776. #define DEBUG_MEM                5
  777. /**
  778.  * Memory allocation debugging macro.
  779.  *
  780.  * This macro is used for debugging output related to the memory
  781.  * allocation subsystem.  It maps to DPRINTF1() so that mem-related
  782.  * debugging output will occur at debug level 5 and higher.
  783.  *
  784.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  785.  * @ingroup DOXGRP_DEBUG
  786.  */
  787. #if DEBUG >= DEBUG_MEM
  788. #  define D_MEM_IF               if (DEBUG_LEVEL >= DEBUG_MEM)
  789. #  define D_MEM(x)               do { D_MEM_IF {DPRINTF(x);} } while (0)
  790. #else
  791. #  define D_MEM_IF               if (0)
  792. #  define D_MEM(x)               NOP
  793. #endif
  794.  
  795. /** Set strings module debugging to level 9999.  @see @link DOXGRP_DEBUG Debugging Subsystem @endlink */
  796. #define DEBUG_STRINGS            9999
  797. /**
  798.  * String routine debugging macro.
  799.  *
  800.  * This macro is used for debugging output related to the string
  801.  * manipulation subsystem.  It maps to D_NEVER() so that
  802.  * string-related debugging output can only be activated manually.
  803.  *
  804.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  805.  * @ingroup DOXGRP_DEBUG
  806.  */
  807. #if DEBUG >= DEBUG_STRINGS
  808. #  define D_STRINGS_IF           if (DEBUG_LEVEL >= DEBUG_STRINGS)
  809. #  define D_STRINGS(x)           do { D_STRINGS_IF {DPRINTF(x);} } while (0)
  810. #else
  811. #  define D_STRINGS_IF           if (0)
  812. #  define D_STRINGS(x)           NOP
  813. #endif
  814. /** Set lexer/parser debugging to level 9999.  @see @link DOXGRP_DEBUG Debugging Subsystem @endlink */
  815. #define DEBUG_PARSE              9999
  816. /**
  817.  * Lexer/parser debugging macro.
  818.  *
  819.  * This macro is used for debugging output related to the lexer/parser
  820.  * portion of the config parser.  It maps to D_NEVER() so that
  821.  * parser-related debugging output can only be activated manually.
  822.  *
  823.  * @see @link DOXGRP_DEBUG Debugging Subsystem @endlink
  824.  * @ingroup DOXGRP_DEBUG
  825.  */
  826. #if DEBUG >= DEBUG_PARSE
  827. #  define D_PARSE_IF             if (DEBUG_LEVEL >= DEBUG_PARSE)
  828. #  define D_PARSE(x)             do { D_PARSE_IF {DPRINTF(x);} } while (0)
  829. #else
  830. #  define D_PARSE_IF             if (0)
  831. #  define D_PARSE(x)             NOP
  832. #endif
  833.  
  834.  
  835.  
  836. /********************************* MEM GOOP ***********************************/
  837. /**
  838.  * @def MALLOC(sz)
  839.  * Allocate @a sz bytes of memory.
  840.  * 
  841.  * This macro is a replacement for the libc function malloc().  It
  842.  * allocates the specified number of bytes of memory on the heap and
  843.  * returns a pointer to that memory location.  This macro calls libc's
  844.  * malloc() if memory debugging is off, and spifmem_malloc() if it's
  845.  * on.
  846.  *
  847.  * @param sz The size in bytes of the block of memory to allocate.
  848.  * @return A pointer to the allocated memory.
  849.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  850.  * @ingroup DOXGRP_MEM
  851.  */
  852. /**
  853.  * @def CALLOC(type, n)
  854.  * Allocate enough memory for @a n objects of type @a type.
  855.  * 
  856.  * This macro is a replacement for the libc function calloc().  It
  857.  * allocates a block of memory on the heap large enough to hold @a n
  858.  * objects of type @a type (e.g., a @a type array of size @a n).  The
  859.  * memory area is zeroed out prior to the pointer to it being
  860.  * returned.  This macro calls libc's calloc() if memory debugging is
  861.  * off and spifmem_calloc() if it's on. 
  862.  *
  863.  * @param type The type of object to be allocated (e.g., int).
  864.  * @param n    The number of objects to be allocated.
  865.  * @return A pointer to the allocated memory.
  866.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  867.  * @ingroup DOXGRP_MEM
  868.  */
  869. /**
  870.  * @def REALLOC(mem, sz)
  871.  * Resize the memory block pointed to by @a mem to @a sz bytes.
  872.  *
  873.  * This macro is a replacement for the libc function realloc().  It
  874.  * changes the size of a chunk of memory previously allocated by
  875.  * malloc() or calloc() (or, by extension, the MALLOC()/CALLOC()
  876.  * macros) and returns a pointer to the (possibly moved) memory area.
  877.  * This macro calls libc's realloc() if memory debugging is off and
  878.  * spifmem_realloc() if it's on.
  879.  *
  880.  * @param mem The old pointer whose size will be changed.
  881.  * @param sz  The new size, in bytes, to be allocated.
  882.  * @return The new pointer value, which may or may not differ from the
  883.  *         old value.
  884.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  885.  * @ingroup DOXGRP_MEM
  886.  */
  887. /**
  888.  * @def FREE(ptr)
  889.  * Free a previously-allocated memory block.
  890.  *
  891.  * This macro is a replacement for the libc function free().  It
  892.  * returns the previously-allocated memory block pointed to by @a ptr
  893.  * to the heap.  This macro calls libc's free() if memory debugging is
  894.  * off and spifmem_free() if it's on.  The @a ptr parameter is assigned
  895.  * the value of NULL after it has been freed.
  896.  *
  897.  * @param ptr The pointer to be freed.
  898.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  899.  * @ingroup DOXGRP_MEM
  900.  */
  901. /**
  902.  * @def STRDUP(s)
  903.  * Duplicate a string pointer and return a pointer to the new copy.
  904.  *
  905.  * This macro is a replacement for the libc function strdup().  It
  906.  * allocates a section of memory large enough to hold the string @a s
  907.  * (including the trailing NUL character), copies the contents of @a s
  908.  * into the new buffer, and returns a pointer to the new copy.  This
  909.  * macro calls libc's strdup() of memory debugging is off and
  910.  * spifmem_strdup() if it's on.
  911.  *
  912.  * @param s The string to duplicate.
  913.  * @return A pointer to the newly-created copy.
  914.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  915.  * @ingroup DOXGRP_MEM
  916.  */
  917. /**
  918.  * @def MALLOC_DUMP()
  919.  * Dumps a listing of all allocated pointers along with their sizes
  920.  * and contents in both hex and ASCII.
  921.  *
  922.  * This macro is used to view the status of memory allocated via the
  923.  * LibAST memory management system.  First the pointers used to track
  924.  * allocated memory are dumped (that's what pointer #0 is); then, each
  925.  * allocated pointer is dumped along with its size and contents, the
  926.  * latter being displayed both in hexadecimal form and ASCII form.
  927.  * Non-printable characters are replaced by dots ('.').  You can see
  928.  * a sample of the output in the
  929.  * @link mem_example.c memory management system example @endlink.
  930.  *
  931.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  932.  * @ingroup DOXGRP_MEM
  933.  */
  934. /**
  935.  * @def X_CREATE_PIXMAP(d, win, w, h, depth)
  936.  * Create an X pixmap.
  937.  *
  938.  * This macro is a replacement for the Xlib function XCreatePixmap().
  939.  * It creates a pixmap of the specified size and returns an X resource
  940.  * ID for it.  This macro calls Xlib's XCreatePixmap() if memory
  941.  * debugging is off and spifmem_x_create_pixmap() if it's on.
  942.  *
  943.  * @param d     The X display connection.
  944.  * @param win   The X drawable on whose display the pixmap will be
  945.  *              created.
  946.  * @param w     The width in pixels of the pixmap.
  947.  * @param h     The height in pixels of the pixmap.
  948.  * @param depth The color depth for the pixmap.
  949.  * @return The Pixmap ID for the new pixmap.
  950.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  951.  * @ingroup DOXGRP_MEM
  952.  */
  953. /**
  954.  * @def X_FREE_PIXMAP(d, p)
  955.  * Free the specified X pixmap.
  956.  *
  957.  * This macro is a replacement for the Xlib function XFreePixmap().
  958.  * It frees the specified pixmap.  This macro calls Xlib's
  959.  * XFreePixmap() if memory debugging is off and spifmem_x_free_pixmap()
  960.  * if it's on.
  961.  *
  962.  * @param d The X display connection.
  963.  * @param p The Pixmap to be freed.
  964.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  965.  * @ingroup DOXGRP_MEM
  966.  */
  967. /**
  968.  * @def IMLIB_REGISTER_PIXMAP(p)
  969.  * Register a pixmap generated by Imlib2 so LibAST can track it.
  970.  *
  971.  * Unfortunately, there is no easy way to wrap all the different ways
  972.  * Imlib2 could conceivably create an image.  So instead, simply use
  973.  * this macro to register the pixmaps Imlib2 creates.  Then LibAST
  974.  * will be able to track them.  This macro calls
  975.  * spifmem_imlib_register_pixmap() if memory debugging is on and if
  976.  * Imlib2 support has been enabled.  Otherwise, it's a NOP().
  977.  *
  978.  * @param p The Pixmap Imlib2 created.
  979.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  980.  * @ingroup DOXGRP_MEM
  981.  */
  982. /**
  983.  * @def IMLIB_FREE_PIXMAP(p)
  984.  * Free a pixmap (and its mask) generated by Imlib2.
  985.  *
  986.  * Once an Imlib2-generated pixmap has been registered, you should
  987.  * use this macro to free it.  It calls spifmem_imlib_free_pixmap() if
  988.  * Imlib2 support has been enabled.  Otherwise, it's a NOP().
  989.  *
  990.  * @param p The Imlib2-generated Pixmap to be freed.
  991.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  992.  * @ingroup DOXGRP_MEM
  993.  */
  994. /**
  995.  * @def PIXMAP_DUMP()
  996.  * Dump a listing of allocated pixmaps.
  997.  *
  998.  * This macro is analogous to the MALLOC_DUMP() macro; rather than
  999.  * dumping a list of pointers, however, it dumps a list of allocated
  1000.  * pixmaps.  Like MALLOC_DUMP(), this macro is a NOP() if memory
  1001.  * debugging support has not been compiled into LibAST.
  1002.  *
  1003.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  1004.  * @ingroup DOXGRP_MEM
  1005.  */
  1006. /**
  1007.  * @def X_CREATE_GC(d, win, f, gcv)
  1008.  * Create an X graphics context.
  1009.  *
  1010.  * This macro is a replacement for the Xlib function XCreateGC().  It
  1011.  * creates a graphics context (GC) object and returns its X resource
  1012.  * ID.  This macro calls Xlib's XCreateGC() if memory debugging is
  1013.  * off and spifmem_x_create_gc() if it's on.
  1014.  *
  1015.  * @param d   The X display connection.
  1016.  * @param win The X drawable on whose screen the GC will be created.
  1017.  * @param f   The GC flags noting which members of @a gcv have set
  1018.  *            values.
  1019.  * @param gcv The GCValues structure defining properties of the GC.
  1020.  * @return The ID of the new GC.
  1021.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  1022.  * @ingroup DOXGRP_MEM
  1023.  */
  1024. /**
  1025.  * @def X_FREE_GC(d, gc)
  1026.  * Free an X graphics context.
  1027.  *
  1028.  * This macro is a replacement for the Xlib function XFreeGC().  It
  1029.  * frees a previously allocated graphics context (GC) object.  This
  1030.  * macro calls Xlib's XFreeGC() if memory debugging is off and
  1031.  * spifmem_x_free_gc() if it's on.
  1032.  *
  1033.  * @param d  The X display connection.
  1034.  * @param gc The graphics context object to free.
  1035.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  1036.  * @ingroup DOXGRP_MEM
  1037.  */
  1038. /**
  1039.  * @def GC_DUMP()
  1040.  * Dump a list of allocated graphics context objects.
  1041.  *
  1042.  * This macro is analogous to the MALLOC_DUMP() macro; rather than
  1043.  * dumping a list of pointers, however, it dumps a list of allocated
  1044.  * GC's.  Like MALLOC_DUMP(), this macro is a NOP() if memory
  1045.  * debugging support has not been compiled into LibAST.
  1046.  *
  1047.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  1048.  * @ingroup DOXGRP_MEM
  1049.  */
  1050. /**
  1051.  * @def MALLOC_MOD
  1052.  * MALLOC() call count interval.
  1053.  *
  1054.  * LibAST has the ability to count calls to MALLOC(); this defines the
  1055.  * interval for reporting the call count.  The default is 25, meaning
  1056.  * that LibAST will print the current count every 25 calls.  Note that
  1057.  * MALLOC_CALL_DEBUG must be defined when compiling LibAST, in
  1058.  * addition to memory debugging, for this feature to work.
  1059.  *
  1060.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  1061.  * @ingroup DOXGRP_MEM
  1062.  */
  1063. /**
  1064.  * @def REALLOC_MOD
  1065.  * REALLOC() call count interval.
  1066.  *
  1067.  * LibAST has the ability to count calls to REALLOC(); this defines
  1068.  * the interval for reporting the call count.  The default is 25,
  1069.  * meaning that LibAST will print the current count every 25 calls.
  1070.  * Note that MALLOC_CALL_DEBUG must be defined when compiling LibAST,
  1071.  * in addition to memory debugging, for this feature to work.
  1072.  *
  1073.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  1074.  * @ingroup DOXGRP_MEM
  1075.  */
  1076. /**
  1077.  * @def CALLOC_MOD
  1078.  * CALLOC() call count interval.
  1079.  *
  1080.  * LibAST has the ability to count calls to CALLOC(); this defines the
  1081.  * interval for reporting the call count.  The default is 25, meaning
  1082.  * that LibAST will print the current count every 25 calls.  Note that
  1083.  * MALLOC_CALL_DEBUG must be defined when compiling LibAST, in
  1084.  * addition to memory debugging, for this feature to work.
  1085.  *
  1086.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  1087.  * @ingroup DOXGRP_MEM
  1088.  */
  1089. /**
  1090.  * @def FREE_MOD
  1091.  * FREE() call count interval.
  1092.  *
  1093.  * LibAST has the ability to count calls to FREE(); this defines the
  1094.  * interval for reporting the call count.  The default is 25, meaning
  1095.  * that LibAST will print the current count every 25 calls.  Note that
  1096.  * MALLOC_CALL_DEBUG must be defined when compiling LibAST, in
  1097.  * addition to memory debugging, for this feature to work.
  1098.  *
  1099.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  1100.  * @ingroup DOXGRP_MEM
  1101.  */
  1102. #if (DEBUG >= DEBUG_MEM)
  1103. # define MALLOC(sz)                             spifmem_malloc(SPIF_CAST(charptr) __FILE__, __LINE__, (sz))
  1104. # define CALLOC(type,n)                         spifmem_calloc(SPIF_CAST(charptr) __FILE__, __LINE__, (n), (sizeof(type)))
  1105. # define REALLOC(mem,sz)                        spifmem_realloc(SPIF_CAST(charptr) #mem, SPIF_CAST(charptr) __FILE__, __LINE__, (mem), (sz))
  1106. # define FREE(ptr)                              do { spifmem_free(SPIF_CAST(charptr) #ptr, SPIF_CAST(charptr) __FILE__, __LINE__, (ptr)); (ptr) = NULL; } while (0)
  1107. # define STRDUP(s)                              spifmem_strdup(SPIF_CAST(charptr) #s, SPIF_CAST(charptr) __FILE__, __LINE__, SPIF_CAST(charptr) (s))
  1108. # define MALLOC_DUMP()                          spifmem_dump_mem_tables()
  1109. # define X_CREATE_PIXMAP(d, win, w, h, depth)   spifmem_x_create_pixmap(SPIF_CAST(charptr) __FILE__, __LINE__, (d), (win), (w), (h), (depth))
  1110. # define X_FREE_PIXMAP(d, p)                    spifmem_x_free_pixmap(SPIF_CAST(charptr) #p, SPIF_CAST(charptr) __FILE__, __LINE__, (d), (p))
  1111. # if LIBAST_IMLIB2_SUPPORT
  1112. #  define IMLIB_REGISTER_PIXMAP(p)              spifmem_imlib_register_pixmap(SPIF_CAST(charptr) #p, SPIF_CAST(charptr) __FILE__, __LINE__, (p))
  1113. #  define IMLIB_FREE_PIXMAP(p)                  spifmem_imlib_free_pixmap(SPIF_CAST(charptr) #p, SPIF_CAST(charptr) __FILE__, __LINE__, (p))
  1114. # else
  1115. #  define IMLIB_REGISTER_PIXMAP(p)              NOP
  1116. #  define IMLIB_FREE_PIXMAP(p)                  NOP
  1117. # endif
  1118. # define PIXMAP_DUMP()                          spifmem_dump_pixmap_tables()
  1119. # define X_CREATE_GC(d, win, f, gcv)            spifmem_x_create_gc(__FILE__, __LINE__, (d), (win), (f), (gcv))
  1120. # define X_FREE_GC(d, gc)                       spifmem_x_free_gc(#gc, __FILE__, __LINE__, (d), (gc))
  1121. # define GC_DUMP()                              spifmem_dump_gc_tables()
  1122. # define MALLOC_MOD 25
  1123. # define REALLOC_MOD 25
  1124. # define CALLOC_MOD 25
  1125. # define FREE_MOD 25
  1126. #else
  1127. # define MALLOC(sz)                             malloc(sz)
  1128. # define CALLOC(type,n)                         calloc((n),(sizeof(type)))
  1129. # define REALLOC(mem,sz)                        ((sz) ? ((mem) ? (realloc((mem), (sz))) : (malloc(sz))) : ((mem) ? (free(mem), NULL) : (NULL)))
  1130. # define FREE(ptr)                              do { free(ptr); (ptr) = NULL; } while (0)
  1131. # define STRDUP(s)                              strdup((char *) s)
  1132. # define MALLOC_DUMP()                          NOP
  1133. # define X_CREATE_PIXMAP(d, win, w, h, depth)   XCreatePixmap((d), (win), (w), (h), (depth))
  1134. # define X_FREE_PIXMAP(d, p)                    XFreePixmap((d), (p))
  1135. # ifdef LIBAST_IMLIB2_SUPPORT
  1136. #  define IMLIB_REGISTER_PIXMAP(p)              NOP
  1137. #  define IMLIB_FREE_PIXMAP(p)                  imlib_free_pixmap_and_mask(p)
  1138. # else
  1139. #  define IMLIB_REGISTER_PIXMAP(p)              NOP
  1140. #  define IMLIB_FREE_PIXMAP(p)                  NOP
  1141. # endif
  1142. # define PIXMAP_DUMP()                          NOP
  1143. # define X_CREATE_GC(d, win, f, gcv)            XCreateGC((d), (win), (f), (gcv))
  1144. # define X_FREE_GC(d, gc)                       XFreeGC((d), (gc))
  1145. # define GC_DUMP()                              NOP
  1146. #endif
  1147.  
  1148. /* Fast memset() macro contributed by vendu */
  1149. #if !defined(SIZEOF_LONG) || (SIZEOF_LONG == 8)
  1150. /** UNDOCUMENTED */
  1151. # define MEMSET_LONG() (l |= l<<32)
  1152. #else
  1153. /** UNDOCUMENTED */
  1154. # define MEMSET_LONG() NOP
  1155. #endif
  1156.  
  1157. /**
  1158.  * @def MEMSET(s, c, count)
  1159.  * Initialize a memory region to a particular value.
  1160.  *
  1161.  * This macro is a replacement for the libc function memset().  It
  1162.  * initializes the memory region pointed to by @a s to the value
  1163.  * specified by @a c.  The size of the memory region is specified by
  1164.  * @a count.  Note that @a c must be a byte (char) value.
  1165.  *
  1166.  * This macro has been optimized to set as many bytes simultaneously as
  1167.  * the architecture can handle, so it should offer superior
  1168.  * performance to libc's memset() function.
  1169.  *
  1170.  * @param s     A pointer to the memory region to initialize.
  1171.  * @param c     The value to which all bytes in the block will be
  1172.  *              set.
  1173.  * @param count The size, in bytes, of the memory region.
  1174.  * @see @link DOXGRP_MEM Memory Management Subsystem @endlink
  1175.  * @ingroup DOXGRP_MEM
  1176.  */
  1177. #define MEMSET(s, c, count) do { \
  1178.     char *end = (char *)(s) + (count); \
  1179.     long l; \
  1180.     long *l_dest = (long *)(s); \
  1181.     char *c_dest; \
  1182.  \
  1183.     if (!(s)) { \
  1184.         break; \
  1185.     } \
  1186.     /* areas of less than 4 * sizeof(long) are set in 1-byte chunks. */ \
  1187.     if (((unsigned long) count) >= 4 * sizeof(long)) { \
  1188.         /* fill l with c. */ \
  1189.         l = (c) | (c)<<8; \
  1190.         l |= l<<16; \
  1191.         MEMSET_LONG(); \
  1192.  \
  1193.         /* fill in 1-byte chunks until boundary of long is reached. */ \
  1194.         if ((unsigned long)l_dest & (unsigned long)(sizeof(long) -1)) { \
  1195.             c_dest = (char *)l_dest; \
  1196.             while ((unsigned long)c_dest & (unsigned long)(sizeof(long) -1)) { \
  1197.                 *(c_dest++) = (c); \
  1198.             } \
  1199.             l_dest = (long *)c_dest; \
  1200.         } \
  1201.  \
  1202.         /* fill in long-size chunks as long as possible. */ \
  1203.         while (((unsigned long) (end - (char *)l_dest)) >= sizeof(long)) { \
  1204.             *(l_dest++) = l; \
  1205.         } \
  1206.     } \
  1207.  \
  1208.     /* fill the tail in 1-byte chunks. */ \
  1209.     if ((char *)l_dest < end) { \
  1210.         c_dest = (char *)l_dest; \
  1211.         *(c_dest++) = (c); \
  1212.         while (c_dest < end) { \
  1213.             *(c_dest++) = (c); \
  1214.         } \
  1215.     } \
  1216.   } while (0)
  1217.  
  1218.  
  1219.  
  1220. /******************************* STRINGS GOOP *********************************/
  1221. /**
  1222.  * Returns the length of a literal string.
  1223.  *
  1224.  * This macro is like libc's strlen() function, except that it
  1225.  * requires the string parameter be a literal rather than a variable.
  1226.  * This makes calculating the string length for a literal easy without
  1227.  * incurring the speed penalty of a call to strlen().
  1228.  *
  1229.  * @param x The literal string (i.e., a fixed string in quotes, like
  1230.  *          "this.").
  1231.  * @return The length of the string.
  1232.  * @see @link DOXGRP_STRINGS String Utility Routines @endlink
  1233.  * @ingroup DOXGRP_STRINGS
  1234.  */
  1235. #define CONST_STRLEN(x)            (sizeof(x) - 1)
  1236. /**
  1237.  * Compares the beginning of a string with a literal.
  1238.  *
  1239.  * This macro, like the libc str*cmp() functions, returns an integer
  1240.  * less than, equal to, or greater than zero depending on if the
  1241.  * initial part of string @a s is found to be less than, to match, or
  1242.  * to be greater than the literal string.  Generally, this is used as
  1243.  * a boolean value (as !BEG_STRCASECMP()) to determine whether or not
  1244.  * @a s starts with @a constr or not.  Note that case is ignored, as
  1245.  * the name implies.
  1246.  *
  1247.  * @param s      The string variable to compare to.
  1248.  * @param constr A literal string representing what should be the
  1249.  *               beginning of @a s.
  1250.  * @return See above.
  1251.  * @see @link DOXGRP_STRINGS String Utility Routines @endlink
  1252.  * @ingroup DOXGRP_STRINGS
  1253.  */
  1254. #define BEG_STRCASECMP(s, constr)  (strncasecmp(SPIF_CAST_C(char *) (s), constr, CONST_STRLEN(constr)))
  1255.  
  1256.  
  1257.  
  1258. /******************************** CONF GOOP ***********************************/
  1259. /**
  1260.  * @def PATH_MAX
  1261.  * The maximum length of a path specifier.
  1262.  *
  1263.  * LibAST requires PATH_MAX to be properly defined.  Unfortunately,
  1264.  * some UNIX versions (namely HP-UX) define it incorrectly.  Most UNIX
  1265.  * versions support a PATH_MAX of 1024, but all must support at least
  1266.  * 255.  So if PATH_MAX is defined to be less than 255 (like HP-UX and
  1267.  * its absolutely ludicrous value of 14), LibAST forceably redefines
  1268.  * it to be 255.
  1269.  * @see @link DOXGRP_CONF Configuration File Parser @endlink
  1270.  * @ingroup DOXGRP_CONF
  1271.  */
  1272. #if defined(PATH_MAX) && (PATH_MAX < 255)
  1273. #  undef PATH_MAX
  1274. #endif
  1275. #ifndef PATH_MAX
  1276. #  define PATH_MAX 255
  1277. #endif
  1278.  
  1279. /**
  1280.  * Maximum length of a line in a config file.
  1281.  *
  1282.  * At no time during parsing can any line in a config file exceed this
  1283.  * length (20 kB by default).
  1284.  * @see @link DOXGRP_CONF Configuration File Parser @endlink
  1285.  * @ingroup DOXGRP_CONF
  1286.  */
  1287. #define CONFIG_BUFF                     20480
  1288.  
  1289. /**
  1290.  * Special flag character.
  1291.  *
  1292.  * This is the special character value passed to a config context
  1293.  * parser when the @c begin statement for that context is
  1294.  * encountered.
  1295.  *
  1296.  * @see @link DOXGRP_CONF Configuration File Parser @endlink
  1297.  * @ingroup DOXGRP_CONF
  1298.  */
  1299. #define SPIFCONF_BEGIN_CHAR                 '\001'
  1300. /**
  1301.  * Special flag character string.
  1302.  *
  1303.  * This is the string representation of SPIFCONF_BEGIN_CHAR.
  1304.  *
  1305.  * @see @link DOXGRP_CONF Configuration File Parser @endlink
  1306.  * @ingroup DOXGRP_CONF
  1307.  */
  1308. #define SPIFCONF_BEGIN_STRING               "\001"
  1309. /**
  1310.  * Special flag character.
  1311.  *
  1312.  * This is the special character value passed to a config context
  1313.  * parser when the @c end statement for that context is encountered.
  1314.  *
  1315.  * @see @link DOXGRP_CONF Configuration File Parser @endlink
  1316.  * @ingroup DOXGRP_CONF
  1317.  */
  1318. #define SPIFCONF_END_CHAR                   '\002'
  1319. /**
  1320.  * Special flag character string.
  1321.  *
  1322.  * This is the string representation of SPIFCONF_END_CHAR.
  1323.  *
  1324.  * @see @link DOXGRP_CONF Configuration File Parser @endlink
  1325.  * @ingroup DOXGRP_CONF
  1326.  */
  1327. #define SPIFCONF_END_STRING                 "\002"
  1328.  
  1329. /**
  1330.  * Compares boolean option value to allowed true values.
  1331.  *
  1332.  * This macro compares the value of a boolean option against the
  1333.  * acceptable boolean "true" values ("1", "on", "yes", and "true").
  1334.  *
  1335.  * @param s String value of a boolean option.
  1336.  * @return Non-zero if a match is found, zero if not.
  1337.  * @see @link DOXGRP_CONF Configuration File Parser @endlink
  1338.  * @ingroup DOXGRP_CONF
  1339.  */
  1340. #define BOOL_OPT_ISTRUE(s)  (!strcasecmp(SPIF_CHARPTR_C(s), true_vals[0]) \
  1341.                              || !strcasecmp(SPIF_CHARPTR_C(s), true_vals[1]) \
  1342.                              || !strcasecmp(SPIF_CHARPTR_C(s), true_vals[2]) \
  1343.                              || !strcasecmp(SPIF_CHARPTR_C(s), true_vals[3]))
  1344. /**
  1345.  * Compares boolean option value to allowed false values.
  1346.  *
  1347.  * This macro compares the value of a boolean option against the
  1348.  * acceptable boolean "false" values ("0", "off", "no", and "false").
  1349.  *
  1350.  * @param s String value of a boolean option.
  1351.  * @return Non-zero if a match is found, zero if not.
  1352.  * @see @link DOXGRP_CONF Configuration File Parser @endlink
  1353.  * @ingroup DOXGRP_CONF
  1354.  */
  1355. #define BOOL_OPT_ISFALSE(s) (!strcasecmp(SPIF_CHARPTR_C(s), false_vals[0]) \
  1356.                              || !strcasecmp(SPIF_CHARPTR_C(s), false_vals[1]) \
  1357.                              || !strcasecmp(SPIF_CHARPTR_C(s), false_vals[2]) \
  1358.                              || !strcasecmp(SPIF_CHARPTR_C(s), false_vals[3]))
  1359.  
  1360. /**
  1361.  * Skip-to-end flag.
  1362.  *
  1363.  * This symbol represents the bit in the FSS flags which specifies
  1364.  * that the parser should skip the rest of the file.
  1365.  *
  1366.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1367.  * @ingroup DOXGRP_CONF_FSS
  1368.  */
  1369. #define FILE_SKIP_TO_END           (0x01)
  1370. /**
  1371.  * Preprocessing flag.
  1372.  *
  1373.  * This symbol represents the bit in the FSS flags which specifies
  1374.  * that this file should be preprocessed.
  1375.  *
  1376.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1377.  * @ingroup DOXGRP_CONF_FSS
  1378.  */
  1379. #define FILE_PREPROC               (0x02)
  1380. /**
  1381.  * Push info for a new file onto the state stack.
  1382.  *
  1383.  * This macro adds a new file state structure to the top of the stack
  1384.  * and populates it with the information contained in the macro
  1385.  * parameters.  When a new file is opened for parsing, a call is made
  1386.  * to this macro to "push" the new file onto the top of the stack.
  1387.  *
  1388.  * @param f  The file pointer (FILE *) representing the newly-opened
  1389.  *           file.
  1390.  * @param p  The path to the newly-opened file.
  1391.  * @param o  The output file name (for preprocessing).
  1392.  * @param l  The current line number for the file.
  1393.  * @param fl The flag set for the file.
  1394.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1395.  * @ingroup DOXGRP_CONF_FSS
  1396.  */
  1397. #define file_push(f, p, o, l, fl)  spifconf_register_fstate(f, p, o, l, fl)
  1398. /**
  1399.  * Pop a state structure off the stack.
  1400.  *
  1401.  * This macro pops a file state structure off the top of the stack.  A
  1402.  * call to this macro occurs once the parsing of the current file is
  1403.  * completed.
  1404.  *
  1405.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1406.  * @ingroup DOXGRP_CONF_FSS
  1407.  */
  1408. #define file_pop()                 (fstate_idx--)
  1409. /**
  1410.  * Return the top file state structure on the stack.
  1411.  *
  1412.  * This macro is used to access the file state structure currently on
  1413.  * top of the stack.
  1414.  *
  1415.  * @return The file state structure atop the stack.
  1416.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1417.  * @ingroup DOXGRP_CONF_FSS
  1418.  */
  1419. #define file_peek()                (fstate[fstate_idx])
  1420. /**
  1421.  * Examine the file pointer on top of the stack.
  1422.  *
  1423.  * This macro returns the file pointer (FILE *) corresponding to the
  1424.  * file currently being parsed.
  1425.  *
  1426.  * @return The current file pointer.
  1427.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1428.  * @ingroup DOXGRP_CONF_FSS
  1429.  */
  1430. #define file_peek_fp()             (fstate[fstate_idx].fp)
  1431. /**
  1432.  * Examine the path of the current file.
  1433.  *
  1434.  * This macro returns the path for the file currently being parsed.
  1435.  *
  1436.  * @return The path of the current file.
  1437.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1438.  * @ingroup DOXGRP_CONF_FSS
  1439.  */
  1440. #define file_peek_path()           (fstate[fstate_idx].path)
  1441. /**
  1442.  * Examine the path of the current pre-processing output file.
  1443.  *
  1444.  * This macro returns the path for the preprocessing output file
  1445.  * currently being parsed.
  1446.  *
  1447.  * @return The path of the current preproc output file.
  1448.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1449.  * @ingroup DOXGRP_CONF_FSS
  1450.  */
  1451. #define file_peek_outfile()        (fstate[fstate_idx].outfile)
  1452. /**
  1453.  * Examine the line number of the current file.
  1454.  *
  1455.  * This macro returns the current line number within the current
  1456.  * config file.
  1457.  *
  1458.  * @return The line number of the current file.
  1459.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1460.  * @ingroup DOXGRP_CONF_FSS
  1461.  */
  1462. #define file_peek_line()           (fstate[fstate_idx].line)
  1463. /**
  1464.  * Check whether or not we're skipping to the end of the current
  1465.  * file.
  1466.  *
  1467.  * This macro returns zero if the current file is being parsed and
  1468.  * non-zero if the parser is skipping to its end.
  1469.  *
  1470.  * @return The skip-to-end flag for the current file.
  1471.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1472.  * @ingroup DOXGRP_CONF_FSS
  1473.  */
  1474. #define file_peek_skip()           (fstate[fstate_idx].flags & FILE_SKIP_TO_END)
  1475. /**
  1476.  * Check whether or not the current file was preprocessed.
  1477.  *
  1478.  * This macro returns zero if the current file was not preprocessed
  1479.  * and non-zero if it was.
  1480.  *
  1481.  * @return The preprocessing flag for the current file.
  1482.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1483.  * @ingroup DOXGRP_CONF_FSS
  1484.  */
  1485. #define file_peek_preproc()        (fstate[fstate_idx].flags & FILE_PREPROC)
  1486.  
  1487. /**
  1488.  * Set the file pointer for the current file.
  1489.  *
  1490.  * @internal
  1491.  * @param f The file pointer (FILE *).
  1492.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1493.  * @ingroup DOXGRP_CONF_FSS
  1494.  */
  1495. #define file_poke_fp(f)            ((fstate[fstate_idx].fp) = (f))
  1496. /**
  1497.  * Set the path for the current file.
  1498.  *
  1499.  * @internal
  1500.  * @param p The path.
  1501.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1502.  * @ingroup DOXGRP_CONF_FSS
  1503.  */
  1504. #define file_poke_path(p)          ((fstate[fstate_idx].path) = (p))
  1505. /**
  1506.  * Set the outfile for the current file.
  1507.  *
  1508.  * @internal
  1509.  * @param o The outfile.
  1510.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1511.  * @ingroup DOXGRP_CONF_FSS
  1512.  */
  1513. #define file_poke_outfile(o)       ((fstate[fstate_idx].outfile) = (o))
  1514. /**
  1515.  * Set the current line number for the current file.
  1516.  *
  1517.  * @internal
  1518.  * @param l The line number.
  1519.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1520.  * @ingroup DOXGRP_CONF_FSS
  1521.  */
  1522. #define file_poke_line(l)          ((fstate[fstate_idx].line) = (l))
  1523. /**
  1524.  * Set the skip-to-end flag for the current file.
  1525.  *
  1526.  * @internal
  1527.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1528.  * @ingroup DOXGRP_CONF_FSS
  1529.  */
  1530. #define file_skip_to_end()         ((fstate[fstate_idx].flags) |= (FILE_SKIP_TO_END))
  1531. /**
  1532.  * Set/clear the skip-to-end flag for the current file.
  1533.  *
  1534.  * @internal
  1535.  * @param s 0 to clear, non-zero to set.
  1536.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1537.  * @ingroup DOXGRP_CONF_FSS
  1538.  */
  1539. #define file_poke_skip(s)          do {if (s) {fstate[fstate_idx].flags |= FILE_SKIP_TO_END;} else {fstate[fstate_idx].flags &= ~(FILE_SKIP_TO_END);} } while (0)
  1540. /**
  1541.  * Set/clear the preprocessing flag for the current file.
  1542.  *
  1543.  * @internal
  1544.  * @param s 0 to clear, non-zero to set.
  1545.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1546.  * @ingroup DOXGRP_CONF_FSS
  1547.  */
  1548. #define file_poke_preproc(s)       do {if (s) {fstate[fstate_idx].flags |= FILE_PREPROC;} else {fstate[fstate_idx].flags &= ~(FILE_PREPROC);} } while (0)
  1549. /**
  1550.  * Set all state info for the current file.
  1551.  *
  1552.  * @internal
  1553.  * @param f  The file pointer (FILE *).
  1554.  * @param p  The file path.
  1555.  * @param o  The outfile.
  1556.  * @param l  The line number.
  1557.  * @param fl The flags.
  1558.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1559.  * @ingroup DOXGRP_CONF_FSS
  1560.  */
  1561. #define file_poke(f, p, o, l, fl)  do {file_poke_fp(f); file_poke_path(p); file_poke_outfile(o); file_poke_line(l); fstate[fstate_idx].flags = (fl);} while (0)
  1562.  
  1563. /**
  1564.  * Increment the line number for the current file.
  1565.  *
  1566.  * @internal
  1567.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1568.  * @ingroup DOXGRP_CONF_FSS
  1569.  */
  1570. #define file_inc_line()            (fstate[fstate_idx].line++)
  1571.  
  1572. /**
  1573.  * File state stack structure.
  1574.  *
  1575.  * This structure comprises the individual stack elements on the file
  1576.  * state stack.  One of these structures is present on the stack for
  1577.  * each file being parsed.
  1578.  *
  1579.  * @see @link DOXGRP_CONF_FSS File State Stack @endlink
  1580.  * @ingroup DOXGRP_CONF_FSS
  1581.  */
  1582. typedef struct file_state_struct {
  1583.     /**
  1584.      * File pointer.
  1585.      *
  1586.      * Contains an open file pointer used to read data from the
  1587.      * file.
  1588.      */
  1589.     FILE *fp;
  1590.     /**
  1591.      * File path.
  1592.      *
  1593.      * Contains the path to the file.
  1594.      */
  1595.     spif_charptr_t path;
  1596.     /**
  1597.      * Preprocessing output file.
  1598.      *
  1599.      * Contains the path to the file used for preprocessing
  1600.      * output.
  1601.      */
  1602.     spif_charptr_t outfile;
  1603.     /**
  1604.      * Line number.
  1605.      *
  1606.      * Contains the current line number for the file.
  1607.      */
  1608.     spif_uint32_t line;
  1609.     /**
  1610.      * File state flags.
  1611.      *
  1612.      * Contains the skip-to-end (FILE_SKIP_TO_END) and preprocessing
  1613.      * (FILE_PREPROC) flags for the file.
  1614.      */
  1615.     spif_uint8_t flags;
  1616. } fstate_t;
  1617.  
  1618. /**
  1619.  * Typedef for pointers to context handler functions.
  1620.  *
  1621.  * This function pointer type is used for variables, typecasts,
  1622.  * etc. involving context handler functions.  Context handlers must
  1623.  * accept two parameters, a char * containing either the config file
  1624.  * line or a begin/end magic string, and a void * containing state
  1625.  * information; they must return a void * which will be passed to the
  1626.  * next invocation of the handler as the aforementioned state
  1627.  * information parameter.
  1628.  *
  1629.  * @see @link DOXGRP_CONF_CTX Context Handling @endlink
  1630.  * @ingroup DOXGRP_CONF_CTX
  1631.  */
  1632. typedef spif_ptr_t (*ctx_handler_t)(spif_charptr_t, spif_ptr_t);
  1633. /**
  1634.  * Typedef for pointers to built-in functions.
  1635.  *
  1636.  * This function pointer type is used for config file built-in
  1637.  * function handlers.  LibAST supplies several built-in functions
  1638.  * which can be used in config files (%get(), %appname(), etc.);
  1639.  * client programs can add their own as well.  Built-in functions take
  1640.  * a single char * parameter, the parameter list passed to the
  1641.  * built-in function in the config file.  They return a char *, the
  1642.  * result string to substitute for the function call.
  1643.  *
  1644.  * @see @link DOXGRP_CONF Configuration File Parser @endlink
  1645.  * @ingroup DOXGRP_CONF
  1646.  */
  1647. typedef spif_charptr_t (*spifconf_func_ptr_t) (spif_charptr_t);
  1648.  
  1649. extern fstate_t *fstate;
  1650. extern unsigned char fstate_idx;
  1651. extern const char *true_vals[], *false_vals[];
  1652.  
  1653.  
  1654. /******************************* OPTIONS GOOP **********************************/
  1655.  
  1656. /*@{*/
  1657. /**
  1658.  * @name Option Flags
  1659.  * Flags for individual options.
  1660.  *
  1661.  * Each option structure (spifopt_t_struct) has a 16-bit value called
  1662.  * "flags" associated with it.  The lowest 5 bits (0-4) are for option
  1663.  * types which do not require a specific value (such as boolean).  The
  1664.  * next 5 bits (5-9) are for option types which always require a
  1665.  * value.  Bit 10 is for abstract options, which could by definition
  1666.  * go either way.  And the final 5 bits (11-15) are for
  1667.  * non-type-related information, such as flagging preparsed or
  1668.  * deprecated options.
  1669.  *
  1670.  * @ingroup DOXGRP_OPT
  1671.  */
  1672.  
  1673. /** No flags.  No flags. */
  1674. #define SPIFOPT_FLAG_NONE                 (0)
  1675. /** Boolean option.  This flag marks a boolean option. */
  1676. #define SPIFOPT_FLAG_BOOLEAN              (1UL << 0)
  1677. /** Counter option.  This flag marks a counter option. */
  1678. #define SPIFOPT_FLAG_COUNTER              (1UL << 1)
  1679. /** No-value type mask.  This is a bitmask to select the lower 5 bits
  1680.  *  (those which represent options where no value is required).
  1681.  */
  1682. #define SPIFOPT_FLAG_TYPEMASK_NOVALUE     (0x001f)
  1683. /** Integer option.  This flag marks an integer (numeric) option. */
  1684. #define SPIFOPT_FLAG_INTEGER              (1UL << 5)
  1685. /** String option.  This flag marks a string option. */
  1686. #define SPIFOPT_FLAG_STRING               (1UL << 6)
  1687. /** Argument list option.  This flag marks an argument list option
  1688.  *  (such as -e/--exec).  There can be one of these at most.
  1689.  */
  1690. #define SPIFOPT_FLAG_ARGLIST              (1UL << 7)
  1691. /** Value type mask.  This is a bitmask to select bits 5-9
  1692.  *  (those which represent options where a value is required).
  1693.  */
  1694. #define SPIFOPT_FLAG_TYPEMASK_VALUE       (0x03e0)
  1695. /** Abstract option.  This flag marks an abstract (client-handled)
  1696.  *  option.
  1697.  */
  1698. #define SPIFOPT_FLAG_ABSTRACT             (1UL << 10)
  1699. /** Type mask.  This is a bitmask to select all type-identifying
  1700.  *  option flag bits (0-10, inclusive).
  1701.  */
  1702. #define SPIFOPT_FLAG_TYPEMASK             (0x07ff)
  1703. /**
  1704.  * Preparsed option.  This flag marks an option which is preparsed
  1705.  * (i.e., parsed only on the pre-parse pass, which is generally done
  1706.  * before config files are read).
  1707.  */
  1708. #define SPIFOPT_FLAG_PREPARSE             (1UL << 11)
  1709. /**
  1710.  * Deprecated option.  This flag marks an option which has been
  1711.  * deprecated by the author(s) of the program.  A warning message is
  1712.  * printed whenever a deprecated option is encountered.
  1713.  */
  1714. #define SPIFOPT_FLAG_DEPRECATED           (1UL << 12)
  1715. /**
  1716.  * Array option.  This flag marks an option which, rather than taking
  1717.  * a single value of its type, takes multiple values of its type.
  1718.  * LibAST will allocate and return a NULL-terminated C-style array of
  1719.  * the given type.  The absence of this flag, except for abstract
  1720.  * options, means that multiple instances of a given flag will
  1721.  * overwrite previous values, if any.  Abstract options are entirely
  1722.  * client-handled, so this flag has no meaning there.
  1723.  */
  1724. #define SPIFOPT_FLAG_ARRAY                (1UL << 13)
  1725. /** Modifier mask.  This is a bitmask to select all the non-type
  1726.  *  flags; i.e., those which modify option behavior.
  1727.  */
  1728. #define SPIFOPT_FLAG_MODMASK              (0xf800)
  1729. /*@}*/
  1730.  
  1731. /*@{*/
  1732. /**
  1733.  * @name Parser Settings
  1734.  * Flags which alter the behavior of the parser itself.
  1735.  *
  1736.  * The option parser settings structure (spifopt_settings_t_struct)
  1737.  * has an 8-bit flag field which contains toggles affecting the
  1738.  * parser's internal behavior.  As a general rule, these will not be
  1739.  * flags that client programs will want to manipulate.  In the event
  1740.  * that you do wish to manipulate these flags, use the
  1741.  * SPIFOPT_FLAGS_*() macros.
  1742.  *
  1743.  * @ingroup DOXGRP_OPT
  1744.  */
  1745.  
  1746. /** 
  1747.  * Preparse flag.  This flag denotes whether or not the next call to
  1748.  * the spifopt_parse() function will parse only those options which
  1749.  * have the SPIFOPT_FLAG_PREPARSE flag set, after which it will clear
  1750.  * this flag.  Callers wishing to have certain options pre-parsed
  1751.  * should set this flag prior to invoking spifopt_parse().  Use of
  1752.  * this flag is not required.
  1753.  */
  1754. #define SPIFOPT_SETTING_PREPARSE         (1UL << 0)
  1755. /*@}*/
  1756.  
  1757. /*@{*/
  1758. /**
  1759.  * @name Option Declaration Convenience Macros
  1760.  * Macros which simplify the building of the options list.
  1761.  *
  1762.  * Each client program which intends to use the LibAST option parser
  1763.  * (i.e., calls spifopt_parse() one or more times) is responsible for
  1764.  * building its own option structure (spifopt_t) array which is then
  1765.  * registered with LibAST using the SPIFOPT_OPTLIST_SET() macro.
  1766.  *
  1767.  * To simplify the declaration of this structure, a set of convenience
  1768.  * macros is provided that will make the structure more
  1769.  * human-readable.  Although their use is not required, it is
  1770.  * recommended in order to enhance readability and reduce the
  1771.  * probability of human error.
  1772.  *
  1773.  * @ingroup DOXGRP_OPT
  1774.  */
  1775.  
  1776. /**
  1777.  * Primary option declaration macro.
  1778.  *
  1779.  * This is the primary macro used for declaring an option.  All other
  1780.  * option declaration convenience macros are merely simpler forms of
  1781.  * this macro.  If you prefer, you can use this macro for all option
  1782.  * declarations; just be sure to get the parameters right.
  1783.  *
  1784.  * @param s The short form as a char, or 0 for none.
  1785.  * @param l The long form as a char *. (required)
  1786.  * @param d The description as a char *. (required)
  1787.  * @param f Bitwise OR of zero or more flags.
  1788.  * @param p The pointer to where the data should be stored.  Except in
  1789.  *          the case of function pointers (for abstract options), this
  1790.  *          parameter will need the & operator.  See the other
  1791.  *          convenience macros for type requirements.
  1792.  * @param m The bitmask for a boolean option, or 0 for other types.
  1793.  */
  1794. #define SPIFOPT_OPTION(s, l, d, f, p, m)  { s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), f, p, m }
  1795. /**
  1796.  * Declare a boolean option.
  1797.  *
  1798.  * This macro is used to declare a simple boolean option with both a
  1799.  * short and a long form.
  1800.  *
  1801.  * @param s The short form as a char, or 0 for none.
  1802.  * @param l The long form as a char *. (required)
  1803.  * @param d The description as a char *. (required)
  1804.  * @param v A variable of type "unsigned long" (or one that can be
  1805.  *          safely typecast to/from it) to be used as a bitfield.
  1806.  * @param m The bitmask to be set/unset within the bitfield.
  1807.  * @see SPIFOPT_OPTION()
  1808.  */
  1809. #define SPIFOPT_BOOL(s, l, d, v, m) \
  1810.     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_BOOLEAN), &(v), m)
  1811. /**
  1812.  * Declare a pre-parsed boolean option.
  1813.  *
  1814.  * This macro is used to declare a boolean option with both a short
  1815.  * and a long form which will be pre-parsed.
  1816.  *
  1817.  * @param s The short form as a char, or 0 for none.
  1818.  * @param l The long form as a char *. (required)
  1819.  * @param d The description as a char *. (required)
  1820.  * @param v A variable of type "unsigned long" (or one that can be
  1821.  *          safely typecast to/from it) to be used as a bitfield.
  1822.  * @param m The bitmask to be set/unset within the bitfield.
  1823.  * @see SPIFOPT_OPTION()
  1824.  */
  1825. #define SPIFOPT_BOOL_PP(s, l, d, v, m) \
  1826.     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_BOOLEAN | SPIFOPT_FLAG_PREPARSE), &(v), m)
  1827. /**
  1828.  * Declare a long-only boolean option.
  1829.  *
  1830.  * This macro is used to declare a boolean option with only a long
  1831.  * form.
  1832.  *
  1833.  * @param l The long form as a char *. (required)
  1834.  * @param d The description as a char *. (required)
  1835.  * @param v A variable of type "unsigned long" (or one that can be
  1836.  *          safely typecast to/from it) to be used as a bitfield.
  1837.  * @param m The bitmask to be set/unset within the bitfield.
  1838.  * @see SPIFOPT_OPTION()
  1839.  */
  1840. #define SPIFOPT_BOOL_LONG(l, d, v, m) \
  1841.     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_BOOLEAN), &(v), m)
  1842. /**
  1843.  * Declare a long-only, pre-parsed boolean option.
  1844.  *
  1845.  * This macro is used to declare a boolean option with only a long
  1846.  * form which will be pre-parsed.
  1847.  *
  1848.  * @param l The long form as a char *. (required)
  1849.  * @param d The description as a char *. (required)
  1850.  * @param v A variable of type "unsigned long" (or one that can be
  1851.  *          safely typecast to/from it) to be used as a bitfield.
  1852.  * @param m The bitmask to be set/unset within the bitfield.
  1853.  * @see SPIFOPT_OPTION()
  1854.  */
  1855. #define SPIFOPT_BOOL_LONG_PP(l, d, v, m) \
  1856.     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_BOOLEAN | SPIFOPT_FLAG_PREPARSE), &(v), m)
  1857. /**
  1858.  * Declare an integer option.
  1859.  *
  1860.  * This macro is used to declare a simple integer option with both a
  1861.  * short and a long form.
  1862.  *
  1863.  * @param s The short form as a char, or 0 for none.
  1864.  * @param l The long form as a char *. (required)
  1865.  * @param d The description as a char *. (required)
  1866.  * @param v A variable of type "int" (or one that can be
  1867.  *          safely typecast to/from it) to store the option value.
  1868.  * @see SPIFOPT_OPTION()
  1869.  */
  1870. #define SPIFOPT_INT(s, l, d, v) \
  1871.     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_INTEGER), &(v), 0)
  1872. /**
  1873.  * Declare a pre-parsed integer option.
  1874.  *
  1875.  * This macro is used to declare an integer option with both a short
  1876.  * and a long form which will be pre-parsed.
  1877.  *
  1878.  * @param s The short form as a char, or 0 for none.
  1879.  * @param l The long form as a char *. (required)
  1880.  * @param d The description as a char *. (required)
  1881.  * @param v A variable of type "int" (or one that can be
  1882.  *          safely typecast to/from it) to store the option value.
  1883.  * @see SPIFOPT_OPTION()
  1884.  */
  1885. #define SPIFOPT_INT_PP(s, l, d, v) \
  1886.     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_INTEGER | SPIFOPT_FLAG_PREPARSE), &(v), 0)
  1887. /**
  1888.  * Declare a long-only integer option.
  1889.  *
  1890.  * This macro is used to declare an integer option with only a long
  1891.  * form.
  1892.  *
  1893.  * @param l The long form as a char *. (required)
  1894.  * @param d The description as a char *. (required)
  1895.  * @param v A variable of type "int" (or one that can be
  1896.  *          safely typecast to/from it) to store the option value.
  1897.  * @see SPIFOPT_OPTION()
  1898.  */
  1899. #define SPIFOPT_INT_LONG(l, d, v) \
  1900.     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_INTEGER), &(v), 0)
  1901. /**
  1902.  * Declare a long-only, pre-parsed integer option.
  1903.  *
  1904.  * This macro is used to declare an integer option with only a long
  1905.  * form which will be pre-parsed.
  1906.  *
  1907.  * @param l The long form as a char *. (required)
  1908.  * @param d The description as a char *. (required)
  1909.  * @param v A variable of type "int" (or one that can be
  1910.  *          safely typecast to/from it) to store the option value.
  1911.  * @see SPIFOPT_OPTION()
  1912.  */
  1913. #define SPIFOPT_INT_LONG_PP(l, d, v) \
  1914.     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_INTEGER | SPIFOPT_FLAG_PREPARSE), &(v), 0)
  1915. /**
  1916.  * Declare a string option.
  1917.  *
  1918.  * This macro is used to declare a simple string option with both a
  1919.  * short and a long form.
  1920.  *
  1921.  * @param s The short form as a char, or 0 for none.
  1922.  * @param l The long form as a char *. (required)
  1923.  * @param d The description as a char *. (required)
  1924.  * @param v A variable of type "const char *" (or one that can be
  1925.  *          safely typecast to/from it) to store the option value.
  1926.  * @see SPIFOPT_OPTION()
  1927.  */
  1928. #define SPIFOPT_STR(s, l, d, v) \
  1929.     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_STRING), &(v), 0)
  1930. /**
  1931.  * Declare a pre-parsed string option.
  1932.  *
  1933.  * This macro is used to declare a string option with both a short and
  1934.  * a long form which will be pre-parsed.
  1935.  *
  1936.  * @param s The short form as a char, or 0 for none.
  1937.  * @param l The long form as a char *. (required)
  1938.  * @param d The description as a char *. (required)
  1939.  * @param v A variable of type "const char *" (or one that can be
  1940.  *          safely typecast to/from it) to store the option value.
  1941.  * @see SPIFOPT_OPTION()
  1942.  */
  1943. #define SPIFOPT_STR_PP(s, l, d, v) \
  1944.     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_STRING | SPIFOPT_FLAG_PREPARSE), &(v), 0)
  1945. /**
  1946.  * Declare a long-only string option.
  1947.  *
  1948.  * This macro is used to declare a string option with only a long
  1949.  * form.
  1950.  *
  1951.  * @param l The long form as a char *. (required)
  1952.  * @param d The description as a char *. (required)
  1953.  * @param v A variable of type "const char *" (or one that can be
  1954.  *          safely typecast to/from it) to store the option value.
  1955.  * @see SPIFOPT_OPTION()
  1956.  */
  1957. #define SPIFOPT_STR_LONG(l, d, v) \
  1958.     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_STRING), &(v), 0)
  1959. /**
  1960.  * Declare a long-only, pre-parsed string option.
  1961.  *
  1962.  * This macro is used to declare a string option with only a long form
  1963.  * which will be pre-parsed.
  1964.  *
  1965.  * @param l The long form as a char *. (required)
  1966.  * @param d The description as a char *. (required)
  1967.  * @param v A variable of type "const char *" (or one that can be
  1968.  *          safely typecast to/from it) to store the option value.
  1969.  * @see SPIFOPT_OPTION()
  1970.  */
  1971. #define SPIFOPT_STR_LONG_PP(l, d, v) \
  1972.     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_STRING | SPIFOPT_FLAG_PREPARSE), &(v), 0)
  1973. /**
  1974.  * Declare an argument list option.
  1975.  *
  1976.  * This macro is used to declare a simple argument list option with
  1977.  * both a short and a long form.
  1978.  *
  1979.  * @note Due to the nature of this option type and the fact that it
  1980.  *       can consume values to the end of the command line, only one
  1981.  *       option of this type can be declared.
  1982.  *
  1983.  * @param s The short form as a char, or 0 for none.
  1984.  * @param l The long form as a char *. (required)
  1985.  * @param d The description as a char *. (required)
  1986.  * @param p A pointer of type "char **" (or one that can be
  1987.  *          safely typecast to/from it) to store the NULL-terminated
  1988.  *          argument list.
  1989.  * @see SPIFOPT_OPTION()
  1990.  */
  1991. #define SPIFOPT_ARGS(s, l, d, p) \
  1992.     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ARGLIST), &(p), 0)
  1993. /**
  1994.  * Declare a pre-parsed argument list option.
  1995.  *
  1996.  * This macro is used to declare an argument list option with both a
  1997.  * short and a long form which will be pre-parsed.
  1998.  *
  1999.  * @note Due to the nature of this option type and the fact that it
  2000.  *       can consume values to the end of the command line, only one
  2001.  *       option of this type can be declared.
  2002.  *
  2003.  * @param s The short form as a char, or 0 for none.
  2004.  * @param l The long form as a char *. (required)
  2005.  * @param d The description as a char *. (required)
  2006.  * @param p A pointer of type "char **" (or one that can be
  2007.  *          safely typecast to/from it) to store the NULL-terminated
  2008.  *          argument list.
  2009.  * @see SPIFOPT_OPTION()
  2010.  */
  2011. #define SPIFOPT_ARGS_PP(s, l, d, p) \
  2012.     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ARGLIST | SPIFOPT_FLAG_PREPARSE), &(p), 0)
  2013. /**
  2014.  * Declare a long-only argument list option.
  2015.  *
  2016.  * This macro is used to declare an argument list option with only a
  2017.  * long form.
  2018.  *
  2019.  * @note Due to the nature of this option type and the fact that it
  2020.  *       can consume values to the end of the command line, only one
  2021.  *       option of this type can be declared.
  2022.  *
  2023.  * @param l The long form as a char *. (required)
  2024.  * @param d The description as a char *. (required)
  2025.  * @param p A pointer of type "char **" (or one that can be
  2026.  *          safely typecast to/from it) to store the NULL-terminated
  2027.  *          argument list.
  2028.  * @see SPIFOPT_OPTION()
  2029.  */
  2030. #define SPIFOPT_ARGS_LONG(l, d, p) \
  2031.     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ARGLIST), &(p), 0)
  2032. /**
  2033.  * Declare a long-only, pre-parsed argument list option.
  2034.  *
  2035.  * This macro is used to declare an argument list option with only a
  2036.  * long form which will be pre-parsed.
  2037.  *
  2038.  * @note Due to the nature of this option type and the fact that it
  2039.  *       can consume values to the end of the command line, only one
  2040.  *       option of this type can be declared.
  2041.  *
  2042.  * @param l The long form as a char *. (required)
  2043.  * @param d The description as a char *. (required)
  2044.  * @param p A pointer of type "char **" (or one that can be
  2045.  *          safely typecast to/from it) to store the NULL-terminated
  2046.  *          argument list.
  2047.  * @see SPIFOPT_OPTION()
  2048.  */
  2049. #define SPIFOPT_ARGS_LONG_PP(l, d, p) \
  2050.     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ARGLIST | SPIFOPT_FLAG_PREPARSE), &(p), 0)
  2051. /**
  2052.  * Declare an abstract option.
  2053.  *
  2054.  * This macro is used to declare a simple abstract option with both a
  2055.  * short and a long form.
  2056.  *
  2057.  * @param s The short form as a char, or 0 for none.
  2058.  * @param l The long form as a char *. (required)
  2059.  * @param d The description as a char *. (required)
  2060.  * @param f A function pointer of type "spifopt_abstract_handler_t"
  2061.  *          (or one that can be safely typecast to/from it) to handle
  2062.  *          the parsing of the option.
  2063.  * @see SPIFOPT_OPTION(), spifopt_abstract_handler_t
  2064.  */
  2065. #define SPIFOPT_ABST(s, l, d, f) \
  2066.     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ABSTRACT), SPIF_CAST(ptr) f, 0)
  2067. /**
  2068.  * Declare a pre-parsed abstract option.
  2069.  *
  2070.  * This macro is used to declare an abstract option with both a short
  2071.  * and a long form which will be pre-parsed.
  2072.  *
  2073.  * @param s The short form as a char, or 0 for none.
  2074.  * @param l The long form as a char *. (required)
  2075.  * @param d The description as a char *. (required)
  2076.  * @param f A function pointer of type "spifopt_abstract_handler_t"
  2077.  *          (or one that can be safely typecast to/from it) to handle
  2078.  *          the parsing of the option.
  2079.  * @see SPIFOPT_OPTION(), spifopt_abstract_handler_t
  2080.  */
  2081. #define SPIFOPT_ABST_PP(s, l, d, f) \
  2082.     SPIFOPT_OPTION(s, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ABSTRACT | SPIFOPT_FLAG_PREPARSE), SPIF_CAST(ptr) f, 0)
  2083. /**
  2084.  * Declare a long-only abstract option.
  2085.  *
  2086.  * This macro is used to declare an abstract option with only a long
  2087.  * form.
  2088.  *
  2089.  * @param l The long form as a char *. (required)
  2090.  * @param d The description as a char *. (required)
  2091.  * @param f A function pointer of type "spifopt_abstract_handler_t"
  2092.  *          (or one that can be safely typecast to/from it) to handle
  2093.  *          the parsing of the option.
  2094.  * @see SPIFOPT_OPTION(), spifopt_abstract_handler_t
  2095.  */
  2096. #define SPIFOPT_ABST_LONG(l, d, f) \
  2097.     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ABSTRACT), SPIF_CAST(ptr) f, 0)
  2098. /**
  2099.  * Declare a long-only, pre-parsed abstract option.
  2100.  *
  2101.  * This macro is used to declare an abstract option with only a long
  2102.  * form which will be pre-parsed.
  2103.  *
  2104.  * @param l The long form as a char *. (required)
  2105.  * @param d The description as a char *. (required)
  2106.  * @param f A function pointer of type "spifopt_abstract_handler_t"
  2107.  *          (or one that can be safely typecast to/from it) to handle
  2108.  *          the parsing of the option.
  2109.  * @see SPIFOPT_OPTION(), spifopt_abstract_handler_t
  2110.  */
  2111. #define SPIFOPT_ABST_LONG_PP(l, d, f) \
  2112.     SPIFOPT_OPTION(0, SPIF_CHARPTR(l), SPIF_CHARPTR(d), (SPIFOPT_FLAG_ABSTRACT | SPIFOPT_FLAG_PREPARSE), SPIF_CAST(ptr) f, 0)
  2113. /*@}*/
  2114.  
  2115. /*@{*/
  2116. /**
  2117.  * @name Option Flag Macros
  2118.  * Macros which provide access to option flag information.
  2119.  *
  2120.  * These macros provide access to option type and flag information
  2121.  * while abstracting the actual method of storage and retrieval.  Each
  2122.  * macro takes exactly one parameter, the offset within the options
  2123.  * list.
  2124.  *
  2125.  * @ingroup DOXGRP_OPT
  2126.  */
  2127.  
  2128. /**
  2129.  * Type macro.
  2130.  *
  2131.  * Retrieves the type flag.  Use of this macro is discouraged; use one
  2132.  * of the other macros instead.
  2133.  *
  2134.  * @param n The index of the desired option in the options list.
  2135.  * @return  The type flag (SPIFOPT_FLAG_*)
  2136.  */
  2137. #define SPIFOPT_OPT_TYPE(n)               (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_TYPEMASK)
  2138. /**
  2139.  * Boolean option test.  Tests whether or not the given option is boolean.
  2140.  *
  2141.  * @param n The index of the desired option in the options list.
  2142.  * @return  0 if option is not boolean, non-zero if it is.
  2143.  */
  2144. #define SPIFOPT_OPT_IS_BOOLEAN(n)         (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_BOOLEAN)
  2145. /**
  2146.  * Counter option test.  Tests whether or not the given option is a counter.
  2147.  *
  2148.  * @param n The index of the desired option in the options list.
  2149.  * @return  0 if option is not a counter, non-zero if it is.
  2150.  */
  2151. #define SPIFOPT_OPT_IS_COUNTER(n)         (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_COUNTER)
  2152. /**
  2153.  * Integer option test.  Tests whether or not the given option is an integer.
  2154.  *
  2155.  * @param n The index of the desired option in the options list.
  2156.  * @return  0 if option is not an integer, non-zero if it is.
  2157.  */
  2158. #define SPIFOPT_OPT_IS_INTEGER(n)         (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_INTEGER)
  2159. /**
  2160.  * String option test.  Tests whether or not the given option is a string.
  2161.  *
  2162.  * @param n The index of the desired option in the options list.
  2163.  * @return  0 if option is not a string, non-zero if it is.
  2164.  */
  2165. #define SPIFOPT_OPT_IS_STRING(n)          (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_STRING)
  2166. /**
  2167.  * Argument list option test.  Tests whether or not the given option
  2168.  * is an argument list.
  2169.  *
  2170.  * @param n The index of the desired option in the options list.
  2171.  * @return  0 if option is not an argument list, non-zero if it is.
  2172.  */
  2173. #define SPIFOPT_OPT_IS_ARGLIST(n)         (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_ARGLIST)
  2174. /**
  2175.  * Abstract option test.  Tests whether or not the given option is abstract.
  2176.  *
  2177.  * @param n The index of the desired option in the options list.
  2178.  * @return  0 if option is not abstract, non-zero if it is.
  2179.  */
  2180. #define SPIFOPT_OPT_IS_ABSTRACT(n)        (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_ABSTRACT)
  2181. /**
  2182.  * Preparse option test.  Tests whether or not the given option is to
  2183.  * be preparsed.
  2184.  *
  2185.  * @param n The index of the desired option in the options list.
  2186.  * @return  0 if option is not to be pre-parsed, non-zero if it is.
  2187.  */
  2188. #define SPIFOPT_OPT_IS_PREPARSE(n)        (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_PREPARSE)
  2189. /**
  2190.  * Deprecated option test.  Tests whether or not the given option is deprecated.
  2191.  *
  2192.  * @param n The index of the desired option in the options list.
  2193.  * @return  0 if option is not deprecated, non-zero if it is.
  2194.  */
  2195. #define SPIFOPT_OPT_IS_DEPRECATED(n)      (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_DEPRECATED)
  2196. /**
  2197.  * Array option test.  Tests whether or not the given option is an array.
  2198.  *
  2199.  * @param n The index of the desired option in the options list.
  2200.  * @return  0 if option is not an array, non-zero if it is.
  2201.  */
  2202. #define SPIFOPT_OPT_IS_ARRAY(n)           (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_ARRAY)
  2203. /**
  2204.  * Value required option test.  Tests whether or not the given option
  2205.  * requires a value.
  2206.  *
  2207.  * @param n The index of the desired option in the options list.
  2208.  * @return  0 if option does not require a value, non-zero if it does.
  2209.  */
  2210. #define SPIFOPT_OPT_NEEDS_VALUE(n)        (SPIFOPT_OPT_FLAGS(n) & SPIFOPT_FLAG_TYPEMASK_VALUE)
  2211. /*@}*/
  2212.  
  2213. /*@{*/
  2214. /**
  2215.  * @name Option Structure Access Macros
  2216.  * Macros which provide access to individual structure components.
  2217.  *
  2218.  * These macros provide an easy method for accessing the various
  2219.  * members of a single option structure (spifopt_t_struct) within the
  2220.  * options list without needing to know how the options list, or the
  2221.  * options within it, are stored.
  2222.  *
  2223.  * @ingroup DOXGRP_OPT
  2224.  */
  2225.  
  2226. /**
  2227.  * Short form.  Returns the short form of the option.
  2228.  *
  2229.  * @param n The index of the desired option in the options list.
  2230.  * @return  The short (char) form of the option.
  2231.  */
  2232. #define SPIFOPT_OPT_SHORT(n)              (SPIFOPT_OPTLIST_GET_OPT(n).short_opt)
  2233. /**
  2234.  * Long form.  Returns the long form of the option.
  2235.  *
  2236.  * @param n The index of the desired option in the options list.
  2237.  * @return  The long (const char *) form of the option.
  2238.  */
  2239. #define SPIFOPT_OPT_LONG(n)               (SPIFOPT_OPTLIST_GET_OPT(n).long_opt)
  2240. /**
  2241.  * Description.  Returns the description of the option.
  2242.  *
  2243.  * @param n The index of the desired option in the options list.
  2244.  * @return  The description (const char *) of the option.
  2245.  */
  2246. #define SPIFOPT_OPT_DESC(n)               (SPIFOPT_OPTLIST_GET_OPT(n).desc)
  2247. /**
  2248.  * Flags.  Returns the flags for the option.
  2249.  *
  2250.  * @param n The index of the desired option in the options list.
  2251.  * @return  The flags (spif_uint16_t) for the option.
  2252.  */
  2253. #define SPIFOPT_OPT_FLAGS(n)              (SPIFOPT_OPTLIST_GET_OPT(n).flags)
  2254. /**
  2255.  * Value pointer.  Returns the value pointer for the option.
  2256.  *
  2257.  * @param n The index of the desired option in the options list.
  2258.  * @return  The value pointer (void *) for the option.
  2259.  */
  2260. #define SPIFOPT_OPT_VALUE(n)              (SPIFOPT_OPTLIST_GET_OPT(n).value)
  2261. /**
  2262.  * Bitmask.  Returns the bitmask for the option.
  2263.  *
  2264.  * @param n The index of the desired option in the options list.
  2265.  * @return  The bitmask (spif_uint32_t) for the option.
  2266.  */
  2267. #define SPIFOPT_OPT_MASK(n)               (SPIFOPT_OPTLIST_GET_OPT(n).mask)
  2268. /*@}*/
  2269.  
  2270. /*@{*/
  2271. /**
  2272.  * @name Option Parser Settings Macros
  2273.  * Macros which provide access to the option parser settings.
  2274.  *
  2275.  * These macros provide abstracted access to various settings used by
  2276.  * the option parser.
  2277.  *
  2278.  * @ingroup DOXGRP_OPT
  2279.  */
  2280.  
  2281. /**
  2282.  * Retrieves a single option structure.
  2283.  *
  2284.  * This macro returns a single option structure (spifopt_t_struct)
  2285.  * from the options list.  This is mostly for internal use.
  2286.  *
  2287.  * @param n The index of the desired option in the options list.
  2288.  * @return  The option structure (spifopt_t_struct).
  2289.  */
  2290. #define SPIFOPT_OPTLIST_GET_OPT(n)        (spifopt_settings.opt_list[((n) < (spifopt_settings.num_opts) ? (n) : (0))])
  2291. /**
  2292.  * Sets the option list.
  2293.  *
  2294.  * This macro should be called by client programs to set the option
  2295.  * list.  It must be a spifopt_t * of some type (probably a
  2296.  * spifopt_t []).  Once set, it must not be manipulated directly; only
  2297.  * via the LibAST-supplied macros.
  2298.  *
  2299.  * @param l The options list variable (a spifopt_t pointer or array).
  2300.  */
  2301. #define SPIFOPT_OPTLIST_SET(l)            (spifopt_settings.opt_list = ((spifopt_t *) (l)))
  2302. /**
  2303.  * Obtains the number of options.
  2304.  *
  2305.  * This macro returns the total number of options in the options list.
  2306.  *
  2307.  * @return  The number of options in the list.
  2308.  */
  2309. #define SPIFOPT_NUMOPTS_GET()             (spifopt_settings.num_opts)
  2310. /**
  2311.  * Sets the number of options.
  2312.  *
  2313.  * This macro sets the number of options in the options list.  It
  2314.  * should be called immediately after SPIFOPT_OPTLIST_SET() and
  2315.  * @em before any functions or macros which use the options list.  The
  2316.  * most common way to handle this is to use a fixed array for the
  2317.  * options list and call
  2318.  *
  2319.  * @code
  2320.  * SPIFOPT_NUMOPTS_SET(sizeof(option_list) / sizeof(spifopt_t))
  2321.  * @endcode
  2322.  *
  2323.  * to set the option count.
  2324.  *
  2325.  * @param n The number of elements in the options list.
  2326.  */
  2327. #define SPIFOPT_NUMOPTS_SET(n)            (spifopt_settings.num_opts = (n))
  2328. /**
  2329.  * Obtains the option parser flag settings.
  2330.  *
  2331.  * This macro returns the value of the parser settings flags.  In most
  2332.  * cases, you should use SPIFOPT_FLAGS_IS_SET() instead.
  2333.  *
  2334.  * @return  The value of the parser settings flags.
  2335.  */
  2336. #define SPIFOPT_FLAGS_GET()               (spifopt_settings.flags)
  2337. /**
  2338.  * Sets an option parser settings flag.
  2339.  *
  2340.  * This macro sets one or more parser settings flags.  There are
  2341.  * currently no client-managed parser flags, so you should avoid this
  2342.  * macro.
  2343.  *
  2344.  * @param m The flag (or set of bitwise-or'd flags) to turn on.
  2345.  */
  2346. #define SPIFOPT_FLAGS_SET(m)              (spifopt_settings.flags |= (m))
  2347. /**
  2348.  * Checks whether or not an option parser settings flag is set.
  2349.  *
  2350.  * This macro tests one or more parser settings flags.  There are
  2351.  * currently no client-managed parser flags, so you should avoid this
  2352.  * macro.
  2353.  *
  2354.  * @param m The flag (or set of bitwise-or'd flags) to check.
  2355.  * @return  0 if none of the selected flags are set, non-zero if at
  2356.  *          least one is.
  2357.  */
  2358. #define SPIFOPT_FLAGS_IS_SET(m)           (spifopt_settings.flags & (m))
  2359. /**
  2360.  * Clears an option parser settings flag.
  2361.  *
  2362.  * This macro clears one or more parser settings flags.  There are
  2363.  * currently no client-managed parser flags, so you should avoid this
  2364.  * macro.
  2365.  *
  2366.  * @param m The flag (or set of bitwise-or'd flags) to turn off.
  2367.  */
  2368. #define SPIFOPT_FLAGS_CLEAR(m)            (spifopt_settings.flags &= ~(m))
  2369. /**
  2370.  * Gets the bad option count.
  2371.  *
  2372.  * This macro retrieves the count of bad options encountered during
  2373.  * option parsing.
  2374.  *
  2375.  * @return The number of bad options encountered.
  2376.  */
  2377. #define SPIFOPT_BADOPTS_GET()             (spifopt_settings.bad_opts)
  2378. /**
  2379.  * Sets the bad option count.
  2380.  *
  2381.  * This macro sets the count of bad options encountered during option
  2382.  * parsing.  This macro should not be used by client programs.
  2383.  *
  2384.  * @param n The number of bad options encountered.
  2385.  */
  2386. #define SPIFOPT_BADOPTS_SET(n)            (spifopt_settings.bad_opts = (n))
  2387. /**
  2388.  * Gets the bad option setting.
  2389.  *
  2390.  * This macro retrieves the setting for the number of bad options
  2391.  * allowed before parsing is aborted and the help screen is displayed.
  2392.  *
  2393.  * @return The number of bad options allowed.
  2394.  */
  2395. #define SPIFOPT_ALLOWBAD_GET()            (spifopt_settings.allow_bad)
  2396. /**
  2397.  * Sets the bad option setting.
  2398.  *
  2399.  * This macro sets the number of bad options allowed before parsing is
  2400.  * aborted and the help screen is displayed.  This macro should be
  2401.  * called prior to option parsing.
  2402.  *
  2403.  * @param n The number of bad options allowed.
  2404.  */
  2405. #define SPIFOPT_ALLOWBAD_SET(n)           (spifopt_settings.allow_bad = (n))
  2406. /**
  2407.  * Gets the help handler function pointer.
  2408.  *
  2409.  * This macro retrieves the pointer to the function which will be
  2410.  * called to display program help, due either to a help option or
  2411.  * an excess number of option parsing errors.  If this value is not
  2412.  * set by the client (via the SPIFOPT_HELPHANDLER_SET() macro), the
  2413.  * default handler is the built-in spifopt_usage() function.
  2414.  *
  2415.  * @return The function pointer for the help/usage screen function.
  2416.  */
  2417. #define SPIFOPT_HELPHANDLER               ((spifopt_settings.help_handler) ? (spifopt_settings.help_handler) : (spifopt_usage))
  2418. /**
  2419.  * Sets the help handler function pointer.
  2420.  *
  2421.  * This macro sets the pointer to the function which will be called to
  2422.  * display program help, due either to a help option or an excess
  2423.  * number of option parsing errors.  If the client fails to call this
  2424.  * macro prior to option parsing, the built-in spifopt_usage()
  2425.  * function is used instead.
  2426.  *
  2427.  * @param f A function pointer of type spifopt_helphandler_t.
  2428.  */
  2429. #define SPIFOPT_HELPHANDLER_SET(f)        (spifopt_settings.help_handler = (f))
  2430. /*@}*/
  2431.  
  2432. /**
  2433.  * @name Type Definitions
  2434.  *
  2435.  */
  2436. /*@{*/
  2437. /**
  2438.  * Typedef for help handler function.
  2439.  *
  2440.  * This type is used for declaring/typecasting function pointers which
  2441.  * will be used for help handlers.  Functions used for this should be
  2442.  * declared as returning void (and in reality does not return at all)
  2443.  * and should take either no parameters, or a single char * parameter.
  2444.  *
  2445.  * @see @link DOXGRP_OPT Command Line Option Parser @endlink, SPIFOPT_HELPHANDLER_SET()
  2446.  * @ingroup DOXGRP_OPT
  2447.  */
  2448. typedef void (*spifopt_helphandler_t)();
  2449. /**
  2450.  * Typedef for abstract option handler function.
  2451.  *
  2452.  * This type is used for declaring/typecasting function pointers which
  2453.  * will be used for abstract option handlers.  Abstract options are
  2454.  * those which require special handling; LibAST implements this by
  2455.  * allowing for an arbitrary user-specified function be invoked when
  2456.  * such an option is encountered.  Functions used for this should be
  2457.  * declared as returning void and should take a single char *
  2458.  * parameter (the value of the option, or NULL if it had no value).
  2459.  *
  2460.  * @see @link DOXGRP_OPT Command Line Option Parser @endlink
  2461.  * @ingroup DOXGRP_OPT
  2462.  */
  2463. typedef void (*spifopt_abstract_handler_t)(spif_charptr_t);
  2464.  
  2465. /**
  2466.  * Option structure.
  2467.  *
  2468.  * This is the structure that holds the data for each of the command
  2469.  * line options for which the parser will be looking.  Client programs
  2470.  * must create an array of these structures (a spifopt_t []) and use
  2471.  * the SPIFOPT_OPTLIST_SET() macro to tell LibAST which variable it
  2472.  * is.
  2473.  *
  2474.  * @note This structure and its members should NEVER be accessed
  2475.  * directly; they are documented solely for informational purposes.
  2476.  * The SPIFOPT_* convenience macros provide a streamlined, easy-to-use
  2477.  * abstraction layer for declaring the option list, setting option
  2478.  * parser parameters, and so forth.  Even the internal code uses these
  2479.  * macros!  Consult the macro documentation and the example code for
  2480.  * further assistance.
  2481.  *
  2482.  * @see @link DOXGRP_OPT Command Line Option Parser @endlink, @link opt_example.c example code @endlink
  2483.  * @ingroup DOXGRP_OPT
  2484.  */
  2485. typedef struct spifopt_t_struct {
  2486.     /**
  2487.      * Short form.
  2488.      *
  2489.      * The short (one char) form of the option.
  2490.      */
  2491.     spif_char_t short_opt;
  2492.     /**
  2493.      * Long form.
  2494.      *
  2495.      * The long (string) form of the option.
  2496.      */
  2497.     spif_charptr_t long_opt;
  2498.     /**
  2499.      * Description.
  2500.      *
  2501.      * The (brief) description of the option for the help screen.
  2502.      */
  2503.     spif_charptr_t desc;
  2504.     /**
  2505.      * Option type/attribute flags.
  2506.      *
  2507.      * The type and attribute flags for this option.
  2508.      */
  2509.     spif_uint16_t flags;
  2510.     /**
  2511.      * Value pointer.
  2512.      *
  2513.      * A pointer to where the value for this option should be stored.
  2514.      * Its exact type, and how it is interpreted, depends on the type
  2515.      * of option being defined.
  2516.      */
  2517.     void *value;
  2518.     /**
  2519.      * Boolean bitmask.
  2520.      *
  2521.      * For boolean options, this is the bitmask for the option.  For
  2522.      * other option types, it has no meaning.
  2523.      */
  2524.     spif_uint32_t mask;
  2525. } spifopt_t;
  2526.  
  2527. /**
  2528.  * Option parser settings structure.
  2529.  *
  2530.  * This is the structure that holds the settings and other internal
  2531.  * variables which control how the options parser functions.
  2532.  *
  2533.  * @note This structure and its members should NEVER be accessed
  2534.  * directly; they are documented solely for informational purposes.
  2535.  * The SPIFOPT_* convenience macros provide a streamlined, easy-to-use
  2536.  * abstraction layer for declaring the option list, setting option
  2537.  * parser parameters, and so forth.  Even the internal code uses these
  2538.  * macros!  Consult the macro documentation and the example code for
  2539.  * further assistance.
  2540.  *
  2541.  * @see @link DOXGRP_OPT Command Line Option Parser @endlink, @link opt_example.c example code @endlink
  2542.  * @ingroup DOXGRP_OPT
  2543.  */
  2544. typedef struct spifopt_settings_t_struct {
  2545.     /**
  2546.      * Options list.
  2547.      *
  2548.      * The array of option structures defining the options to look
  2549.      * for.
  2550.      */
  2551.     spifopt_t *opt_list;
  2552.     /**
  2553.      * Option count.
  2554.      *
  2555.      * The total number of options in the options list.
  2556.      */
  2557.     spif_uint16_t num_opts;
  2558.     /**
  2559.      * Parser flags.
  2560.      *
  2561.      * Flags which control the behavior of the parser.
  2562.      */
  2563.     spif_uint8_t flags;
  2564.     /**
  2565.      * Bad option count.
  2566.      *
  2567.      * Keeps track of the number of bad options (i.e., option syntax
  2568.      * errors, such as missing values or unknown options)
  2569.      * encountered.
  2570.      */
  2571.     spif_uint8_t bad_opts;
  2572.     /**
  2573.      * Bad option limit.
  2574.      *
  2575.      * The maximum number of bad options allowed before giving up and
  2576.      * displaying the help text.
  2577.      */
  2578.     spif_uint8_t allow_bad;
  2579.     spif_uint8_t indent; /**< Unused. */
  2580.     /**
  2581.      * Help handler.
  2582.      *
  2583.      * Pointer to the function which is responsible for displaying the
  2584.      * help text.  If undefined, spifopt_usage() is used.
  2585.      */
  2586.     spifopt_helphandler_t help_handler;
  2587. } spifopt_settings_t;
  2588. /*@}*/
  2589.  
  2590. extern spifopt_settings_t spifopt_settings;
  2591.  
  2592.  
  2593. /******************************* HASHING GOOP *********************************/
  2594.  
  2595. /**
  2596.  * @name Jenkins Hash
  2597.  *
  2598.  * Bob Jenkins' hash algorithm as published in December 1996.  Public
  2599.  * domain.  See http://burtleburtle.net/bob/hash/
  2600.  */
  2601. /*@{*/
  2602. /**
  2603.  * Calculate number of hash buckets needed for an n-bit hash key.
  2604.  *
  2605.  * This macro returns the number of hash buckets needed for a hash key
  2606.  * of n distinct bits.  Choose n based on your tolerable level of
  2607.  * collisions, on average, for 2^n key values.
  2608.  *
  2609.  * @param n The number of bits.
  2610.  * @return  Number of hash buckets required.
  2611.  *
  2612.  */
  2613. #define SPIFHASH_SIZE(n)       (SPIF_CAST(uint32) (1UL << (n)))
  2614.  
  2615. /**
  2616.  * Calculate mask to apply to hash key to get lowest n bits.
  2617.  *
  2618.  * This macro returns the bitmask needed for a hash key of n distinct
  2619.  * bits.  Choose n based on your tolerable level of collisions, on
  2620.  * average, for 2^n key values.
  2621.  *
  2622.  * @param n The number of bits.
  2623.  * @return  Bitmask to zero out all but the n lowest bits.
  2624.  *
  2625.  */
  2626. #define SPIFHASH_MASK(n)       (SPIF_CAST(uint32) (SPIFHASH_SIZE(n) - 1))
  2627.  
  2628. /**
  2629.  * Mix 3 32-bit integer values in a reproducible, reversible manner.
  2630.  *
  2631.  * This macro is used by the Jenkins hash algorithm to shuffle bits of
  2632.  * three integers in such a way as to make sure that changes are
  2633.  * propogated throughout the values.
  2634.  *
  2635.  * Instructions are arranged in such a way as to be parallizable;
  2636.  * i.e., excluding the first two instructions, each subsequent pair of
  2637.  * instructions may be evaluated simultaneously for pipelining
  2638.  * purposes.
  2639.  *
  2640.  * @param a A 32-bit integer.
  2641.  * @param b A 32-bit integer.
  2642.  * @param c A 32-bit integer.
  2643.  */
  2644. #define SPIFHASH_JENKINS_MIX(a,b,c) \
  2645. { \
  2646.     a -= b; a -= c; a ^= (c>>13); \
  2647.     b -= c; b -= a; b ^= (a<<8);  \
  2648.     c -= a; c -= b; c ^= (b>>13); \
  2649.     a -= b; a -= c; a ^= (c>>12); \
  2650.     b -= c; b -= a; b ^= (a<<16); \
  2651.     c -= a; c -= b; c ^= (b>>5);  \
  2652.     a -= b; a -= c; a ^= (c>>3);  \
  2653.     b -= c; b -= a; b ^= (a<<10); \
  2654.     c -= a; c -= b; c ^= (b>>15); \
  2655. }
  2656.  
  2657. /**
  2658.  * A pointer to a hash function.
  2659.  *
  2660.  * This type is used to refer to any of the spifhash_* functions.  A
  2661.  * variable of this type can point to any of the built-in hash
  2662.  * functions in LibAST interchangeably.
  2663.  *
  2664.  */
  2665. typedef spif_uint32_t (*spifhash_func_t)(spif_uint8_t *, spif_uint32_t, spif_uint32_t);
  2666. /*@}*/
  2667.  
  2668.  
  2669.  
  2670. /******************************** PROTOTYPES **********************************/
  2671.  
  2672. /* msgs.c */
  2673. extern void libast_set_program_name(const char *);
  2674. extern void libast_set_program_version(const char *);
  2675. extern int libast_dprintf(const char *, ...);
  2676. extern void libast_print_error(const char *fmt, ...);
  2677. extern void libast_print_warning(const char *fmt, ...);
  2678. extern void libast_fatal_error(const char *fmt, ...);
  2679.  
  2680. /* debug.c */
  2681. extern unsigned int DEBUG_LEVEL;
  2682.  
  2683. /* mem.c */
  2684. extern void spifmem_init(void);
  2685. extern void *spifmem_malloc(const spif_charptr_t, unsigned long, size_t);
  2686. extern void *spifmem_realloc(const spif_charptr_t, const spif_charptr_t, unsigned long, void *, size_t);
  2687. extern void *spifmem_calloc(const spif_charptr_t, unsigned long, size_t, size_t);
  2688. extern void spifmem_free(const spif_charptr_t, const spif_charptr_t, unsigned long, void *);
  2689. extern spif_charptr_t spifmem_strdup(const spif_charptr_t, const spif_charptr_t,
  2690.                                      unsigned long, const spif_charptr_t);
  2691. extern void spifmem_dump_mem_tables(void);
  2692. #if LIBAST_X11_SUPPORT
  2693. extern Pixmap spifmem_x_create_pixmap(const spif_charptr_t, unsigned long, Display *,
  2694.                                       Drawable, unsigned int, unsigned int, unsigned int);
  2695. extern void spifmem_x_free_pixmap(const spif_charptr_t, const spif_charptr_t,
  2696.                                   unsigned long, Display *, Pixmap);
  2697. # if LIBAST_IMLIB2_SUPPORT
  2698. extern void spifmem_imlib_register_pixmap(const spif_charptr_t var, const spif_charptr_t filename,
  2699.                                           unsigned long line, Pixmap p);
  2700. extern void spifmem_imlib_free_pixmap(const spif_charptr_t var, const spif_charptr_t filename,
  2701.                                       unsigned long line, Pixmap p);
  2702. # endif
  2703. extern void spifmem_dump_pixmap_tables(void);
  2704. extern GC spifmem_x_create_gc(const spif_charptr_t, unsigned long, Display *, Drawable,
  2705.                               unsigned long, XGCValues *);
  2706. extern void spifmem_x_free_gc(const spif_charptr_t, const spif_charptr_t, unsigned long, Display *, GC);
  2707. extern void spifmem_dump_gc_tables(void);
  2708. #endif
  2709. extern void spiftool_free_array(void *, size_t);
  2710.  
  2711. /* file.c */
  2712. extern int spiftool_temp_file(spif_charptr_t, size_t);
  2713.  
  2714. /* strings.c */
  2715. extern spif_bool_t spiftool_safe_strncpy(spif_charptr_t dest, const spif_charptr_t src, spif_int32_t size);
  2716. extern spif_bool_t spiftool_safe_strncat(spif_charptr_t dest, const spif_charptr_t src, spif_int32_t size);
  2717. extern spif_charptr_t spiftool_substr(const spif_charptr_t, spif_int32_t, spif_int32_t);
  2718. #if LIBAST_REGEXP_SUPPORT_POSIX && HAVE_REGEX_H
  2719. extern spif_bool_t spiftool_regexp_match(const spif_charptr_t, const spif_charptr_t);
  2720. extern spif_bool_t spiftool_regexp_match_r(const spif_charptr_t str, const spif_charptr_t pattern, regex_t **rexp);
  2721. #endif
  2722. extern spif_charptr_t *spiftool_split(const spif_charptr_t, const spif_charptr_t);
  2723. extern spif_charptr_t *spiftool_split_regexp(const spif_charptr_t, const spif_charptr_t);
  2724. extern spif_charptr_t spiftool_join(spif_charptr_t, spif_charptr_t *);
  2725. extern spif_charptr_t spiftool_get_word(unsigned long, const spif_charptr_t);
  2726. extern spif_charptr_t spiftool_get_pword(unsigned long, const spif_charptr_t);
  2727. extern unsigned long spiftool_num_words(const spif_charptr_t);
  2728. extern spif_charptr_t spiftool_chomp(spif_charptr_t);
  2729. extern spif_charptr_t spiftool_downcase_str(spif_charptr_t);
  2730. extern spif_charptr_t spiftool_upcase_str(spif_charptr_t);
  2731. extern spif_charptr_t spiftool_safe_str(spif_charptr_t, unsigned short);
  2732. extern spif_charptr_t spiftool_condense_whitespace(spif_charptr_t);
  2733. extern void spiftool_hex_dump(void *, size_t);
  2734. extern spif_cmp_t spiftool_version_compare(spif_charptr_t, spif_charptr_t);
  2735. #if !(HAVE_MEMMEM)
  2736. extern void *memmem(const void *, size_t, const void *, size_t);
  2737. #endif
  2738. #if !(HAVE_STRNLEN)
  2739. extern size_t strnlen(const char *, size_t);
  2740. #endif
  2741. #if !(HAVE_USLEEP)
  2742. extern void usleep(unsigned long);
  2743. #endif
  2744. #if !(HAVE_SNPRINTF)
  2745. extern int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
  2746. extern int snprintf(char *str, size_t count, const char *fmt, ...);
  2747. #endif
  2748. #if !(HAVE_STRCASESTR)
  2749. extern char *strcasestr(const char *, const char *);
  2750. #endif
  2751. #if !(HAVE_STRCASECHR)
  2752. extern char *strcasechr(const char *, const char);
  2753. #endif
  2754. #if !(HAVE_STRCASEPBRK)
  2755. extern char *strcasepbrk(const char *, const char *);
  2756. #endif
  2757. #if !(HAVE_STRREV)
  2758. extern char *strrev(char *);
  2759. #endif
  2760. #if !(HAVE_STRSEP)
  2761. extern char *strsep(char **, char *);
  2762. #endif
  2763.  
  2764. /* builtin_hashes.c */
  2765. extern spif_uint32_t spifhash_jenkins(register spif_uint8_t *key, register spif_uint32_t length, register spif_uint32_t seed);
  2766. extern spif_uint32_t spifhash_jenkins32(spif_uint8_t *key, register spif_uint32_t length, register spif_uint32_t seed);
  2767. #if WORDS_BIGENDIAN
  2768. #  define spifhash_jenkinsLE(k, l, s)  spifhash_jenkins((k), (l), (s))
  2769. #else
  2770. extern spif_uint32_t spifhash_jenkinsLE(register spif_uint8_t *key, register spif_uint32_t length, register spif_uint32_t seed);
  2771. #endif
  2772. extern spif_uint32_t spifhash_rotating(spif_uint8_t *key, spif_uint32_t len, spif_uint32_t seed);
  2773. extern spif_uint32_t spifhash_one_at_a_time(spif_uint8_t *key, spif_uint32_t len, spif_uint32_t seed);
  2774. extern spif_uint32_t spifhash_fnv(spif_uint8_t *key, spif_uint32_t len, spif_uint32_t seed);
  2775.  
  2776. /* conf.c */
  2777. extern void spifconf_init_subsystem(void);
  2778. extern unsigned char spifconf_register_context(spif_charptr_t name, ctx_handler_t handler);
  2779. extern unsigned char spifconf_register_fstate(FILE *fp, spif_charptr_t path, spif_charptr_t outfile, unsigned long line, unsigned char flags);
  2780. extern unsigned char spifconf_register_builtin(char *name, spifconf_func_ptr_t ptr);
  2781. extern unsigned char spifconf_register_context_state(unsigned char ctx_id);
  2782. extern void spifconf_free_subsystem(void);
  2783. extern spif_charptr_t spifconf_shell_expand(spif_charptr_t);
  2784. extern spif_charptr_t spifconf_find_file(const spif_charptr_t file, const spif_charptr_t dir, const spif_charptr_t pathlist);
  2785. extern FILE *spifconf_open_file(spif_charptr_t name);
  2786. extern void spifconf_parse_line(FILE *fp, spif_charptr_t buff);
  2787. extern spif_charptr_t spifconf_parse(spif_charptr_t conf_name, const spif_charptr_t dir, const spif_charptr_t path);
  2788.  
  2789. /* options.c */
  2790. extern void spifopt_parse(int, char **);
  2791. extern void spifopt_usage(void);
  2792.  
  2793.  
  2794. /* Do we, or do we not, pollute the namespace like we used to? */
  2795. #if LIBAST_COMPAT_05_API
  2796. /* The application must have defined this. */
  2797. /* conf.c */
  2798. # define CONF_BEGIN_CHAR                                         SPIFCONF_BEGIN_CHAR
  2799. # define CONF_END_CHAR                                           SPIFCONF_END_CHAR
  2800. # define CONF_BEGIN_STRING                                       SPIFCONF_BEGIN_STRING
  2801. # define CONF_END_STRING                                         SPIFCONF_END_STRING
  2802. typedef spifconf_func_ptr_t conf_func_ptr_t;
  2803. # define conf_init_subsystem()                                   spifconf_init_subsystem()
  2804. # define conf_register_context(a, b)                             spifconf_register_context((a), (b))
  2805. # define conf_register_fstate(a, b, c, d, e)                     spifconf_register_fstate((a), (b), (c), (d), (e))
  2806. # define conf_register_builtin(a, b)                             spifconf_register_builtin((a), (b))
  2807. # define conf_register_context_state(a)                          spifconf_register_context_state(a)
  2808. # define conf_free_subsystem()                                   spifconf_free_subsystem()
  2809. # define shell_expand(a)                                         spifconf_shell_expand(a)
  2810. # define conf_find_file(a, b, c)                                 spifconf_find_file((a), (b), (c))
  2811. # define open_config_file(a)                                     spifconf_open_file(a)
  2812. # define conf_parse_line(a, b)                                   spifconf_parse_line((a), (b))
  2813. # define conf_parse(a, b, c)                                     spifconf_parse((a), (b), (c))
  2814.  
  2815. /* mem.c */
  2816. # define memrec_init()                                           spifmem_init()
  2817. # define libast_malloc(a, b, c)                                  spifmem_malloc((a), (b), (c))
  2818. # define libast_realloc(a, b, c, d, e)                           spifmem_realloc((a), (b), (c), (d), (e))
  2819. # define libast_calloc(a, b, c, d)                               spifmem_calloc((a), (b), (c), (d))
  2820. # define libast_free(a, b, c, d)                                 spifmem_free((a), (b), (c), (d))
  2821. # define libast_strdup(a, b, c, d)                               spifmem_strdup((a), (b), (c), (d))
  2822. # define libast_dump_mem_tables()                                spifmem_dump_mem_tables()
  2823. # if LIBAST_X11_SUPPORT
  2824. #  define libast_x_create_pixmap(a, b, c, d, e, f, g)            spifmem_x_create_pixmap((a), (b), (c), (d), (e), (f), (g))
  2825. #  define libast_x_free_pixmap(a, b, c, d, e)                    spifmem_x_free_pixmap((a), (b), (c), (d), (e))
  2826. #  if LIBAST_IMLIB2_SUPPORT
  2827. #   define libast_imlib_register_pixmap(a, b, c, d)              spifmem_imlib_register_pixmap((a), (b), (c), (d))
  2828. #   define libast_imlib_free_pixmap(a, b, c, d)                  spifmem_imlib_free_pixmap((a), (b), (c), (d))
  2829. #  endif
  2830. #  define libast_dump_pixmap_tables()                            spifmem_dump_pixmap_tables()
  2831. #  define libast_x_create_gc(a, b, c, d, e, f)                   spifmem_x_create_gc((a), (b), (c), (d), (e), (f))
  2832. #  define libast_x_free_gc(a, b, c, d, e)                        spifmem_x_free_gc((a), (b), (c), (d), (e))
  2833. #  define libast_dump_gc_tables()                                spifmem_dump_gc_tables()
  2834. # endif
  2835. # define free_array(a, b)                                        spiftool_free_array((a), (b))
  2836.  
  2837. /* file.c */
  2838. # define libast_temp_file(a, b)                                  spiftool_temp_file((a), (b))
  2839.  
  2840. /* msgs.c */
  2841. static void (*print_error)(const char *, ...) = libast_print_error;
  2842. static void (*print_warning)(const char *, ...) = libast_print_warning;
  2843. static void (*fatal_error)(const char *, ...) = libast_fatal_error;
  2844.  
  2845. /* strings.c */
  2846. # define regexp_match(a, b)                                      spiftool_regexp_match((a), (b))
  2847. # define regexp_match_r(a, b, c)                                 spiftool_regexp_match_r((a), (b), (c))
  2848. # define split(a, b)                                             spiftool_split((a), (b))
  2849. # define join(a, b)                                              spiftool_join((a), (b))
  2850. # define get_word(a, b)                                          spiftool_get_word((a), (b))
  2851. # define get_pword(a, b)                                         spiftool_get_pword((a), (b))
  2852. # define num_words(a)                                            spiftool_num_words(a)
  2853. # define chomp(a)                                                spiftool_chomp(a)
  2854. # define strip_whitespace(a)                                     spiftool_strip_whitespace(a)
  2855. # define downcase_str(a)                                         spiftool_downcase_str(a)
  2856. # define upcase_str(a)                                           spiftool_upcase_str(a)
  2857. # define safe_str(a, b)                                          spiftool_safe_str((a), (b))
  2858. # define condense_whitespace(a)                                  spiftool_condense_whitespace(a)
  2859. # define hex_dump(a, b)                                          spiftool_hex_dump((a), (b))
  2860. # define version_compare(a, b)                                   spiftool_version_compare((a), (b))
  2861.  
  2862. #endif /* LIBAST_COMPAT_05_API */
  2863.  
  2864. #endif /* _LIBAST_H_ */
  2865.